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