54695
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
1 ;;; url.el --- Uniform Resource Locator retrieval tool
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
2 ;; Author: Bill Perry <wmperry@gnu.org>
|
54699
|
3 ;; Version: $Revision: 1.1.1.1 $
|
54695
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
4 ;; Keywords: comm, data, processes, hypermedia
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
5
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
6 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
7 ;;; Copyright (c) 1993 - 1996 by William M. Perry <wmperry@cs.indiana.edu>
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
8 ;;; Copyright (c) 1996, 97, 98, 99, 2001 Free Software Foundation, Inc.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
9 ;;;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
10 ;;; This file is part of GNU Emacs.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
11 ;;;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
12 ;;; GNU Emacs is free software; you can redistribute it and/or modify
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
13 ;;; it under the terms of the GNU General Public License as published by
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
14 ;;; the Free Software Foundation; either version 2, or (at your option)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
15 ;;; any later version.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
16 ;;;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
17 ;;; GNU Emacs is distributed in the hope that it will be useful,
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
20 ;;; GNU General Public License for more details.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
21 ;;;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
22 ;;; You should have received a copy of the GNU General Public License
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
23 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
24 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
25 ;;; Boston, MA 02111-1307, USA.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
27
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
28 ;; Registered URI schemes: http://www.iana.org/assignments/uri-schemes
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
29
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
30 (eval-when-compile (require 'cl))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
31 ;; Don't require CL at runtime if we can avoid it (Emacs 21).
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
32 ;; Otherwise we need it for hashing functions. `puthash' was never
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
33 ;; defined in the Emacs 20 cl.el for some reason.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
34 (if (fboundp 'puthash)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
35 nil ; internal or CL is loaded
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
36 (defalias 'puthash 'cl-puthash)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
37 (autoload 'cl-puthash "cl")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
38 (autoload 'gethash "cl")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
39 (autoload 'maphash "cl")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
40 (autoload 'make-hash-table "cl"))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
41
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
42 (eval-when-compile
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
43 (require 'mm-decode)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
44 (require 'mm-view))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
45
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
46 (require 'mailcap)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
47 (require 'url-vars)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
48 (require 'url-cookie)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
49 (require 'url-history)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
50 (require 'url-expand)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
51 (require 'url-privacy)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
52 (require 'url-methods)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
53 (require 'url-proxy)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
54 (require 'url-parse)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
55 (require 'url-util)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
56
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
57 ;; Fixme: customize? convert-standard-filename?
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
58 ;;;###autoload
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
59 (defvar url-configuration-directory "~/.url")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
60
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
61 (defun url-do-setup ()
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
62 "Setup the url package.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
63 This is to avoid conflict with user settings if URL is dumped with
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
64 Emacs."
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
65 (unless url-setup-done
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
66
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
67 ;; Make OS/2 happy
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
68 ;;(push '("http" "80") tcp-binary-process-input-services)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
69
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
70 (mailcap-parse-mailcaps)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
71 (mailcap-parse-mimetypes)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
72
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
73 ;; Register all the authentication schemes we can handle
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
74 (url-register-auth-scheme "basic" nil 4)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
75 (url-register-auth-scheme "digest" nil 7)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
76
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
77 (setq url-cookie-file
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
78 (or url-cookie-file
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
79 (expand-file-name "cookies" url-configuration-directory)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
80
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
81 (setq url-history-file
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
82 (or url-history-file
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
83 (expand-file-name "history" url-configuration-directory)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
84
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
85 ;; Parse the global history file if it exists, so that it can be used
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
86 ;; for URL completion, etc.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
87 (url-history-parse-history)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
88 (url-history-setup-save-timer)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
89
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
90 ;; Ditto for cookies
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
91 (url-cookie-setup-save-timer)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
92 (url-cookie-parse-file url-cookie-file)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
93
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
94 ;; Read in proxy gateways
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
95 (let ((noproxy (and (not (assoc "no_proxy" url-proxy-services))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
96 (or (getenv "NO_PROXY")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
97 (getenv "no_PROXY")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
98 (getenv "no_proxy")))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
99 (if noproxy
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
100 (setq url-proxy-services
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
101 (cons (cons "no_proxy"
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
102 (concat "\\("
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
103 (mapconcat
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
104 (lambda (x)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
105 (cond
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
106 ((= x ?,) "\\|")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
107 ((= x ? ) "")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
108 ((= x ?.) (regexp-quote "."))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
109 ((= x ?*) ".*")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
110 ((= x ??) ".")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
111 (t (char-to-string x))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
112 noproxy "") "\\)"))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
113 url-proxy-services))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
114
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
115 ;; Set the password entry funtion based on user defaults or guess
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
116 ;; based on which remote-file-access package they are using.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
117 (cond
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
118 (url-passwd-entry-func nil) ; Already been set
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
119 ((fboundp 'read-passwd) ; Use secure password if available
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
120 (setq url-passwd-entry-func 'read-passwd))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
121 ((or (featurep 'efs) ; Using EFS
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
122 (featurep 'efs-auto)) ; or autoloading efs
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
123 (if (not (fboundp 'read-passwd))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
124 (autoload 'read-passwd "passwd" "Read in a password" nil))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
125 (setq url-passwd-entry-func 'read-passwd))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
126 ((or (featurep 'ange-ftp) ; Using ange-ftp
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
127 (and (boundp 'file-name-handler-alist)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
128 (not (featurep 'xemacs)))) ; ??
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
129 (setq url-passwd-entry-func 'ange-ftp-read-passwd))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
130 (t
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
131 (url-warn
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
132 'security
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
133 "(url-setup): Can't determine how to read passwords, winging it.")))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
134
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
135 (url-setup-privacy-info)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
136 (run-hooks 'url-load-hook)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
137 (setq url-setup-done t)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
138
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
139 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
140 ;;; Retrieval functions
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
141 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
142 (defun url-retrieve (url callback &optional cbargs)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
143 "Retrieve URL asynchronously and call CALLBACK with CBARGS when finished.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
144 The callback is called when the object has been completely retrieved, with
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
145 the current buffer containing the object, and any MIME headers associated
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
146 with it. URL is either a string or a parsed URL.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
147
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
148 Return the buffer URL will load into, or nil if the process has
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
149 already completed."
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
150 (url-do-setup)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
151 (url-gc-dead-buffers)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
152 (if (stringp url)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
153 (set-text-properties 0 (length url) nil url))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
154 (if (not (vectorp url))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
155 (setq url (url-generic-parse-url url)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
156 (if (not (functionp callback))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
157 (error "Must provide a callback function to url-retrieve"))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
158 (unless (url-type url)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
159 (error "Bad url: %s" (url-recreate-url url)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
160 (let ((loader (url-scheme-get-property (url-type url) 'loader))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
161 (url-using-proxy (if (url-host url)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
162 (url-find-proxy-for-url url (url-host url))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
163 (buffer nil)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
164 (asynch (url-scheme-get-property (url-type url) 'asynchronous-p)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
165 (if url-using-proxy
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
166 (setq asynch t
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
167 loader 'url-proxy))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
168 (if asynch
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
169 (setq buffer (funcall loader url callback cbargs))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
170 (setq buffer (funcall loader url))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
171 (if buffer
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
172 (save-excursion
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
173 (set-buffer buffer)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
174 (apply callback cbargs))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
175 (url-history-update-url url (current-time))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
176 buffer))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
177
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
178 (defun url-retrieve-synchronously (url)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
179 "Retrieve URL synchronously.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
180 Return the buffer containing the data, or nil if there are no data
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
181 associated with it (the case for dired, info, or mailto URLs that need
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
182 no further processing). URL is either a string or a parsed URL."
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
183 (url-do-setup)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
184
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
185 (lexical-let ((retrieval-done nil)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
186 (asynch-buffer nil))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
187 (setq asynch-buffer
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
188 (url-retrieve url (lambda (&rest ignored)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
189 (url-debug 'retrieval "Synchronous fetching done (%S)" (current-buffer))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
190 (setq retrieval-done t
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
191 asynch-buffer (current-buffer)))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
192 (if (not asynch-buffer)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
193 ;; We do not need to do anything, it was a mailto or something
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
194 ;; similar that takes processing completely outside of the URL
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
195 ;; package.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
196 nil
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
197 (while (not retrieval-done)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
198 (url-debug 'retrieval "Spinning in url-retrieve-synchronously: %S (%S)"
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
199 retrieval-done asynch-buffer)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
200 ;; Quoth monnier:
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
201 ;; It turns out that the problem seems to be that the (sit-for
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
202 ;; 0.1) below doesn't actually process the data: instead it
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
203 ;; returns immediately because there is keyboard input
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
204 ;; waiting, so we end up spinning endlessly waiting for the
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
205 ;; process to finish while not letting it finish.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
206
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
207 ;; However, raman claims that it blocks Emacs with Emacspeak
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
208 ;; for unexplained reasons. Put back for his benefit until
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
209 ;; someone can understand it.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
210 ;; (sleep-for 0.1)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
211 (sit-for 0.1))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
212 asynch-buffer)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
213
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
214 (defun url-mm-callback (&rest ignored)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
215 (let ((handle (mm-dissect-buffer t)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
216 (save-excursion
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
217 (url-mark-buffer-as-dead (current-buffer))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
218 (set-buffer (generate-new-buffer (url-recreate-url url-current-object)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
219 (if (eq (mm-display-part handle) 'external)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
220 (progn
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
221 (set-process-sentinel
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
222 ;; Fixme: this shouldn't have to know the form of the
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
223 ;; undisplayer produced by `mm-display-part'.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
224 (get-buffer-process (cdr (mm-handle-undisplayer handle)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
225 `(lambda (proc event)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
226 (mm-destroy-parts (quote ,handle))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
227 (message "Viewing externally")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
228 (kill-buffer (current-buffer)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
229 (display-buffer (current-buffer))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
230 (mm-destroy-parts handle)))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
231
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
232 (defun url-mm-url (url)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
233 "Retrieve URL and pass to the appropriate viewing application."
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
234 (require 'mm-decode)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
235 (require 'mm-view)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
236 (url-retrieve url 'url-mm-callback nil))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
237
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
238 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
239 ;;; Miscellaneous
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
240 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
241 (defvar url-dead-buffer-list nil)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
242
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
243 (defun url-mark-buffer-as-dead (buff)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
244 (push buff url-dead-buffer-list))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
245
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
246 (defun url-gc-dead-buffers ()
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
247 (let ((buff))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
248 (while (setq buff (pop url-dead-buffer-list))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
249 (if (buffer-live-p buff)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
250 (kill-buffer buff)))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
251
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
252 (cond
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
253 ((fboundp 'display-warning)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
254 (defalias 'url-warn 'display-warning))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
255 ((fboundp 'warn)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
256 (defun url-warn (class message &optional level)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
257 (warn "(%s/%s) %s" class (or level 'warning) message)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
258 (t
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
259 (defun url-warn (class message &optional level)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
260 (save-excursion
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
261 (set-buffer (get-buffer-create "*URL-WARNINGS*"))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
262 (goto-char (point-max))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
263 (save-excursion
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
264 (insert (format "(%s/%s) %s\n" class (or level 'warning) message)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
265 (display-buffer (current-buffer))))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
266
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
267 (provide 'url)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
268
|
54699
|
269 ;;; arch-tag: bc182f1f-d187-4f10-b961-47af2066579a
|
54695
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
270 ;;; url.el ends here
|