* emulation/pc-select.el (scroll-down-mark, scroll-down-nomark)
[bpt/emacs.git] / lisp / url / url-methods.el
CommitLineData
8c8b8430 1;;; url-methods.el --- Load URL schemes as needed
ffc00a35 2
71ddfde5 3;; Copyright (C) 1996, 1997, 1998, 1999, 2004,
ae940284 4;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
ffc00a35 5
8c8b8430
SM
6;; Keywords: comm, data, processes, hypermedia
7
ffc00a35
SM
8;; This file is part of GNU Emacs.
9
4936186e 10;; GNU Emacs is free software: you can redistribute it and/or modify
ffc00a35 11;; it under the terms of the GNU General Public License as published by
4936186e
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
14
ffc00a35
SM
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
4936186e 19
ffc00a35 20;; You should have received a copy of the GNU General Public License
4936186e 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
ffc00a35
SM
22
23;;; Commentary:
24
25;;; Code:
8c8b8430
SM
26
27(eval-when-compile
28 (require 'cl))
29
30;; This loads up some of the small, silly URLs that I really don't
31;; want to bother putting in their own separate files.
8c8b8430
SM
32(require 'url-parse)
33
34(defvar url-scheme-registry (make-hash-table :size 7 :test 'equal))
35
36(defconst url-scheme-methods
37 '((default-port . variable)
38 (asynchronous-p . variable)
39 (expand-file-name . function)
40 (file-exists-p . function)
41 (file-attributes . function)
42 (parse-url . function)
43 (file-symlink-p . function)
44 (file-writable-p . function)
45 (file-directory-p . function)
46 (file-executable-p . function)
47 (directory-files . function)
48 (file-truename . function))
49 "Assoc-list of methods that each URL loader can provide.")
50
51(defconst url-scheme-default-properties
52 (list 'name "unknown"
53 'loader 'url-scheme-default-loader
54 'default-port 0
55 'expand-file-name 'url-identity-expander
56 'parse-url 'url-generic-parse-url
57 'asynchronous-p nil
58 'file-directory-p 'ignore
59 'file-truename (lambda (&rest args)
60 (url-recreate-url (car args)))
61 'file-exists-p 'ignore
62 'file-attributes 'ignore))
63
64(defun url-scheme-default-loader (url &optional callback cbargs)
65 "Signal an error for an unknown URL scheme."
66 (error "Unkown URL scheme: %s" (url-type url)))
67
68(defun url-scheme-register-proxy (scheme)
69 "Automatically find a proxy for SCHEME and put it in `url-proxy-services'."
70 (let* ((env-var (concat scheme "_proxy"))
71 (env-proxy (or (getenv (upcase env-var))
72 (getenv (downcase env-var))))
73 (cur-proxy (assoc scheme url-proxy-services))
74 (urlobj nil))
75
f316efd9
MB
76 ;; If env-proxy is an empty string, treat it as if it were nil
77 (when (and (stringp env-proxy)
78 (string= env-proxy ""))
79 (setq env-proxy nil))
80
8c8b8430
SM
81 ;; Store any proxying information - this will not overwrite an old
82 ;; entry, so that people can still set this information in their
83 ;; .emacs file
84 (cond
85 (cur-proxy nil) ; Keep their old settings
86 ((null env-proxy) nil) ; No proxy setup
87 ;; First check if its something like hostname:port
88 ((string-match "^\\([^:]+\\):\\([0-9]+\\)$" env-proxy)
89 (setq urlobj (url-generic-parse-url nil)) ; Get a blank object
d18ec89f
SM
90 (setf (url-type urlobj) "http")
91 (setf (url-host urlobj) (match-string 1 env-proxy))
92 (setf (url-port urlobj) (string-to-number (match-string 2 env-proxy))))
8c8b8430
SM
93 ;; Then check if its a fully specified URL
94 ((string-match url-nonrelative-link env-proxy)
95 (setq urlobj (url-generic-parse-url env-proxy))
d18ec89f
SM
96 (setf (url-type urlobj) "http")
97 (setf (url-target urlobj) nil))
8c8b8430
SM
98 ;; Finally, fall back on the assumption that its just a hostname
99 (t
100 (setq urlobj (url-generic-parse-url nil)) ; Get a blank object
d18ec89f
SM
101 (setf (url-type urlobj) "http")
102 (setf (url-host urlobj) env-proxy)))
8c8b8430
SM
103
104 (if (and (not cur-proxy) urlobj)
105 (progn
106 (setq url-proxy-services
107 (cons (cons scheme (format "%s:%d" (url-host urlobj)
108 (url-port urlobj)))
109 url-proxy-services))
110 (message "Using a proxy for %s..." scheme)))))
111
112(defun url-scheme-get-property (scheme property)
d1ce47b0 113 "Get PROPERTY of a URL SCHEME.
8c8b8430
SM
114Will automatically try to load a backend from url-SCHEME.el if
115it has not already been loaded."
116 (setq scheme (downcase scheme))
117 (let ((desc (gethash scheme url-scheme-registry)))
118 (if (not desc)
119 (let* ((stub (concat "url-" scheme))
120 (loader (intern stub)))
121 (condition-case ()
122 (require loader)
123 (error nil))
124 (if (fboundp loader)
125 (progn
126 ;; Found the module to handle <scheme> URLs
127 (url-scheme-register-proxy scheme)
128 (setq desc (list 'name scheme
129 'loader loader))
130 (dolist (cell url-scheme-methods)
131 (let ((symbol (intern-soft (format "%s-%s" stub (car cell))))
132 (type (cdr cell)))
133 (if symbol
134 (case type
135 (function
136 ;; Store the symbol name of a function
137 (if (fboundp symbol)
138 (setq desc (plist-put desc (car cell) symbol))))
139 (variable
140 ;; Store the VALUE of a variable
141 (if (boundp symbol)
142 (setq desc (plist-put desc (car cell)
143 (symbol-value symbol)))))
144 (otherwise
145 (error "Malformed url-scheme-methods entry: %S"
146 cell))))))
147 (puthash scheme desc url-scheme-registry)))))
148 (or (plist-get desc property)
149 (plist-get url-scheme-default-properties property))))
150
151(provide 'url-methods)
e5566bd5 152
ffc00a35
SM
153;; arch-tag: 336863f8-5a07-4906-9be5-b3c6bcebbe67
154;;; url-methods.el ends here