(vc-svn-registered): Catch all errors.
[bpt/emacs.git] / lisp / net / webjump.el
1 ;;; webjump.el --- programmable Web hotlist
2
3 ;; Copyright (C) 1996, 1997, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
5
6 ;; Author: Neil W. Van Dyke <nwv@acm.org>
7 ;; Created: 09-Aug-1996
8 ;; Keywords: comm www
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;; WebJump provides a sort of ``programmable hotlist'' of Web sites that can
30 ;; quickly be invoked in your Web browser. Each Web site in the hotlist has a
31 ;; name, and you select the desired site name via a completing string prompt in
32 ;; the minibuffer. The URL for each Web site is defined as a static string or
33 ;; a built-in or custom function, allowing interactive prompting for
34 ;; site-specific queries and options.
35
36 ;; Note that WebJump was originally intended to complement your conventional
37 ;; browser-based hotlist, not replace it. (Though there's no reason you
38 ;; couldn't use WebJump for your entire hotlist if you were so inclined.)
39
40 ;; The `webjump-sites' variable, which defines the hotlist, defaults to some
41 ;; example sites. You'll probably want to override it with your own favorite
42 ;; sites. The documentation for the variable describes the syntax.
43
44 ;; You may wish to add something like the following to your `.emacs' file:
45 ;;
46 ;; (require 'webjump)
47 ;; (global-set-key "\C-cj" 'webjump)
48 ;; (setq webjump-sites
49 ;; (append '(
50 ;; ("My Home Page" . "www.someisp.net/users/joebobjr/")
51 ;; ("Pop's Site" . "www.joebob-and-son.com/")
52 ;; )
53 ;; webjump-sample-sites))
54 ;;
55 ;; The above loads this package, binds `C-c j' to invoke WebJump, and adds your
56 ;; personal favorite sites to the hotlist.
57
58 ;; The `webjump-sample-sites' variable mostly contains some site entries that
59 ;; are expected to be generally relevant to many users, but excluding
60 ;; those that the GNU project would not want to recommend.
61
62 ;; The `browse-url' package is used to submit URLs to the browser, so any
63 ;; browser-specific configuration should be done there.
64
65 ;;; Code:
66
67 ;;-------------------------------------------------------- Package Dependencies
68
69 (require 'browse-url)
70
71 ;;------------------------------------------------------------------- Constants
72
73 (defvar webjump-sample-sites
74 '(
75
76 ;; FSF, not including Emacs-specific.
77 ("GNU Project FTP Archive" .
78 [mirrors "ftp://ftp.gnu.org/pub/gnu/"
79 ;; ASIA:
80 "ftp://ftp.cs.titech.ac.jp"
81 "ftp://tron.um.u-tokyo.ac.jp/pub/GNU/prep"
82 "ftp://cair-archive.kaist.ac.kr/pub/gnu"
83 "ftp://ftp.nectec.or.th/pub/mirrors/gnu"
84 ;; AUSTRALIA:
85 "ftp://archie.au/gnu"
86 "ftp://archie.oz/gnu"
87 "ftp://archie.oz.au/gnu"
88 ;; AFRICA:
89 "ftp://ftp.sun.ac.za/pub/gnu"
90 ;; MIDDLE-EAST:
91 "ftp://ftp.technion.ac.il/pub/unsupported/gnu"
92 ;; EUROPE:
93 "ftp://irisa.irisa.fr/pub/gnu"
94 "ftp://ftp.univ-lyon1.fr/pub/gnu"
95 "ftp://ftp.mcc.ac.uk"
96 "ftp://unix.hensa.ac.uk/mirrors/uunet/systems/gnu"
97 "ftp://src.doc.ic.ac.uk/gnu"
98 "ftp://ftp.ieunet.ie/pub/gnu"
99 "ftp://ftp.eunet.ch"
100 "ftp://nic.switch.ch/mirror/gnu"
101 "ftp://ftp.informatik.rwth-aachen.de/pub/gnu"
102 "ftp://ftp.informatik.tu-muenchen.de"
103 "ftp://ftp.win.tue.nl/pub/gnu"
104 "ftp://ftp.nl.net"
105 "ftp://ftp.etsimo.uniovi.es/pub/gnu"
106 "ftp://ftp.funet.fi/pub/gnu"
107 "ftp://ftp.denet.dk"
108 "ftp://ftp.stacken.kth.se"
109 "ftp://isy.liu.se"
110 "ftp://ftp.luth.se/pub/unix/gnu"
111 "ftp://ftp.sunet.se/pub/gnu"
112 "ftp://archive.eu.net"
113 ;; SOUTH AMERICA:
114 "ftp://ftp.inf.utfsm.cl/pub/gnu"
115 "ftp://ftp.unicamp.br/pub/gnu"
116 ;; WESTERN CANADA:
117 "ftp://ftp.cs.ubc.ca/mirror2/gnu"
118 ;; USA:
119 "ftp://wuarchive.wustl.edu/systems/gnu"
120 "ftp://labrea.stanford.edu"
121 "ftp://ftp.digex.net/pub/gnu"
122 "ftp://ftp.kpc.com/pub/mirror/gnu"
123 "ftp://f.ms.uky.edu/pub3/gnu"
124 "ftp://jaguar.utah.edu/gnustuff"
125 "ftp://ftp.hawaii.edu/mirrors/gnu"
126 "ftp://uiarchive.cso.uiuc.edu/pub/gnu"
127 "ftp://ftp.cs.columbia.edu/archives/gnu/prep"
128 "ftp://gatekeeper.dec.com/pub/GNU"
129 "ftp://ftp.uu.net/systems/gnu"])
130 ("GNU Project Home Page" . "www.gnu.org")
131
132 ;; Emacs.
133 ("Emacs Lisp Archive" .
134 "ftp://ftp.emacs.org/pub/")
135
136 ;; Internet search engines.
137 ("AltaVista" .
138 [simple-query
139 "www.altavista.digital.com"
140 "www.altavista.digital.com/cgi-bin/query?pg=aq&what=web&fmt=.&q="
141 "&r=&d0=&d1="])
142 ("Archie" .
143 [simple-query "hoohoo.ncsa.uiuc.edu/cgi-bin/AA.pl"
144 "hoohoo.ncsa.uiuc.edu/cgi-bin/AA.pl?query=" ""])
145 ("Lycos" .
146 [simple-query "www.lycos.com"
147 "www.lycos.com/cgi-bin/pursuit?cat=lycos&query=" ""])
148 ("Yahoo" .
149 [simple-query "www.yahoo.com" "search.yahoo.com/bin/search?p=" ""])
150
151 ;; Misc. general interest.
152 ("Interactive Weather Information Network" . webjump-to-iwin)
153 ("Usenet FAQs" .
154 [simple-query "www.cis.ohio-state.edu/hypertext/faq/usenet/FAQ-List.html"
155 "www.cis.ohio-state.edu/htbin/search-usenet-faqs/form?find="
156 ""])
157 ("RTFM Usenet FAQs by Group" .
158 "ftp://rtfm.mit.edu/pub/usenet-by-group/")
159 ("RTFM Usenet FAQs by Hierachy" .
160 "ftp://rtfm.mit.edu/pub/usenet-by-hierarchy/")
161 ("X Consortium Archive" . "ftp.x.org")
162 ("Yahoo: Reference" . "www.yahoo.com/Reference/")
163
164 ;; Computer social issues, privacy, professionalism.
165 ("Association for Computing Machinery" . "www.acm.org")
166 ("Computer Professionals for Social Responsibility" . "www.cpsr.org/dox/")
167 ("Electronic Frontier Foundation" . "www.eff.org")
168 ("IEEE Computer Society" . "www.computer.org")
169 ("Risks Digest" . webjump-to-risks)
170
171 ;; Fun.
172 ("Bastard Operator from Hell" . "www.replay.com/bofh/")
173
174 )
175 "Sample hotlist for WebJump. See the documentation for the `webjump'
176 function and the `webjump-sites' variable.")
177
178 (defvar webjump-state-to-postal-alist
179 '(("Alabama" . "al") ("Alaska" . "ak") ("Arizona" . "az") ("Arkansas" . "ar")
180 ("California" . "ca") ("Colorado" . "co") ("Connecticut" . "ct")
181 ("Delaware" . "de") ("Florida" . "fl") ("Georgia" . "ga") ("Hawaii" . "hi")
182 ("Idaho" . "id") ("Illinois" . "il") ("Indiana" . "in") ("Iowa" . "ia")
183 ("Kansas" . "ks") ("Kentucky" . "ky") ("Louisiana" . "la") ("Maine" . "me")
184 ("Maryland" . "md") ("Massachusetts" . "ma") ("Michigan" . "mi")
185 ("Minnesota" . "mn") ("Mississippi" . "ms") ("Missouri" . "mo")
186 ("Montana" . "mt") ("Nebraska" . "ne") ("Nevada" . "nv")
187 ("New Hampshire" . "nh") ("New Jersey" . "nj") ("New Mexico" . "nm")
188 ("New York" . "ny") ("North Carolina" . "nc") ("North Dakota" . "nd")
189 ("Ohio" . "oh") ("Oklahoma" . "ok") ("Oregon" . "or")
190 ("Pennsylvania" . "pa") ("Rhode Island" . "ri") ("South Carolina" . "sc")
191 ("South Dakota" . "sd") ("Tennessee" . "tn") ("Texas" . "tx")
192 ("Utah" . "ut") ("Vermont" . "vt") ("Virginia" . "va")
193 ("Washington" . "wa") ("West Virginia" . "wv") ("Wisconsin" . "wi")
194 ("Wyoming" . "wy")))
195
196 ;;------------------------------------------------------------ Option Variables
197
198 (defvar webjump-sites
199 webjump-sample-sites
200 "*Hotlist for WebJump.
201
202 The hotlist is represented as an association list, with the CAR of each cell
203 being the name of the Web site, and the CDR being the definition for the URL of
204 that site. The URL definition can be a string (the URL), a vector (specifying
205 a special \"builtin\" which returns a URL), a symbol (name of a function which
206 returns a URL), or a list (which when `eval'ed yields a URL).
207
208 If the URL definition is a vector, then a \"builtin\" is used. A builtin has a
209 Lisp-like syntax, with the name as the first element of the vector, and any
210 arguments as the following elements. The three current builtins are `name',
211 which returns the name of the site as the URL, `simple-query', which
212 returns a URL that is a function of a query entered by the user, and `mirrors',
213 which allows the user to select from among multiple mirror sites for the same
214 content.
215
216 The first argument to the `simple-query' builtin is a static URL to use if the
217 user enters a blank query. The second and third arguments are the prefix and
218 suffix, respectively, to add to the encoded query the user enters. This
219 builtin covers Web sites that have single-string searches with the query
220 embedded in the URL.
221
222 The arguments to the `mirrors' builtin are URLs of mirror sites.
223
224 If the symbol of a function is given, then the function will be called with the
225 Web site name (the one you specified in the CAR of the alist cell) as a
226 parameter. This might come in handy for various kludges.
227
228 For convenience, if the `http://', `ftp://', or `file://' prefix is missing
229 from a URL, WebJump will make a guess at what you wanted and prepend it before
230 submitting the URL.")
231
232 ;;------------------------------------------------------- Sample Site Functions
233
234 (defun webjump-to-iwin (name)
235 (let ((prefix "http://iwin.nws.noaa.gov/")
236 (state (webjump-read-choice name "state"
237 (append '(("Puerto Rico" . "pr"))
238 webjump-state-to-postal-alist))))
239 (if state
240 (concat prefix "iwin/" state "/"
241 (webjump-read-choice name "option"
242 '(("Hourly Report" . "hourly")
243 ("State Forecast" . "state")
244 ("Local Forecast" . "local")
245 ("Zone Forecast" . "zone")
246 ("Short-Term Forecast" . "shortterm")
247 ("Weather Summary" . "summary")
248 ("Public Information" . "public")
249 ("Climatic Data" . "climate")
250 ("Aviation Products" . "aviation")
251 ("Hydro Products" . "hydro")
252 ("Special Weather" . "special")
253 ("Watches and Warnings" . "warnings"))
254 "zone")
255 ".html")
256 prefix)))
257
258 (defun webjump-to-risks (name)
259 (let (issue volume)
260 (if (and (setq volume (webjump-read-number (concat name " volume")))
261 (setq issue (webjump-read-number (concat name " issue"))))
262 (format "catless.ncl.ac.uk/Risks/%d.%02d.html" volume issue)
263 "catless.ncl.ac.uk/Risks/")))
264
265 ;;-------------------------------------------------------------- Core Functions
266
267 ;;;###autoload
268 (defun webjump ()
269 "Jumps to a Web site from a programmable hotlist.
270
271 See the documentation for the `webjump-sites' variable for how to customize the
272 hotlist.
273
274 Please submit bug reports and other feedback to the author, Neil W. Van Dyke
275 <nwv@acm.org>."
276 (interactive)
277 (let* ((completion-ignore-case t)
278 (item (assoc-string
279 (completing-read "WebJump to site: " webjump-sites nil t)
280 webjump-sites t))
281 (name (car item))
282 (expr (cdr item)))
283 (browse-url (webjump-url-fix
284 (cond ((not expr) "")
285 ((stringp expr) expr)
286 ((vectorp expr) (webjump-builtin expr name))
287 ((listp expr) (eval expr))
288 ((symbolp expr)
289 (if (fboundp expr)
290 (funcall expr name)
291 (error "WebJump URL function \"%s\" undefined"
292 expr)))
293 (t (error "WebJump URL expression for \"%s\" invalid"
294 name)))))))
295
296 (defun webjump-builtin (expr name)
297 (if (< (length expr) 1)
298 (error "WebJump URL builtin for \"%s\" empty" name))
299 (let ((builtin (aref expr 0)))
300 (cond
301 ((eq builtin 'mirrors)
302 (if (= (length expr) 1)
303 (error
304 "WebJump URL builtin \"mirrors\" for \"%s\" needs at least 1 arg"
305 name))
306 (webjump-choose-mirror name (cdr (append expr nil))))
307 ((eq builtin 'name)
308 name)
309 ((eq builtin 'simple-query)
310 (webjump-builtin-check-args expr name 3)
311 (webjump-do-simple-query name (aref expr 1) (aref expr 2) (aref expr 3)))
312 (t (error "WebJump URL builtin \"%s\" for \"%s\" invalid"
313 builtin name)))))
314
315 (defun webjump-builtin-check-args (expr name count)
316 (or (= (length expr) (1+ count))
317 (error "WebJump URL builtin \"%s\" for \"%s\" needs %d args"
318 (aref expr 0) name count)))
319
320 (defun webjump-choose-mirror (name urls)
321 (webjump-read-url-choice (concat name " mirror")
322 urls
323 (webjump-mirror-default urls)))
324
325 (defun webjump-do-simple-query (name noquery-url query-prefix query-suffix)
326 (let ((query (webjump-read-string (concat name " query"))))
327 (if query
328 (concat query-prefix (webjump-url-encode query) query-suffix)
329 noquery-url)))
330
331 (defun webjump-mirror-default (urls)
332 ;; Note: This should be modified to apply some simple kludges/heuristics to
333 ;; pick a site which is likely "close". As a tie-breaker among candidates
334 ;; judged equally desirable, randomness might be used.
335 (car urls))
336
337 (defun webjump-read-choice (name what choices &optional default)
338 (let* ((completion-ignore-case t)
339 (choice (completing-read (concat name " " what ": ") choices nil t)))
340 (if (webjump-null-or-blank-string-p choice)
341 default
342 (cdr (assoc choice choices)))))
343
344 (defun webjump-read-number (prompt)
345 ;; Note: I should make this more robust someday.
346 (let ((input (webjump-read-string prompt)))
347 (if input (string-to-number input))))
348
349 (defun webjump-read-string (prompt)
350 (let ((input (read-string (concat prompt ": "))))
351 (if (webjump-null-or-blank-string-p input) nil input)))
352
353 (defun webjump-read-url-choice (what urls &optional default)
354 ;; Note: Convert this to use `webjump-read-choice' someday.
355 (let* ((completions (mapcar (function (lambda (n) (cons n n)))
356 urls))
357 (input (completing-read (concat what
358 ;;(if default " (RET for default)" "")
359 ": ")
360 completions
361 nil
362 t)))
363 (if (webjump-null-or-blank-string-p input)
364 default
365 (car (assoc input completions)))))
366
367 (defun webjump-null-or-blank-string-p (str)
368 (or (null str) (string-match "^[ \t]*$" str)))
369
370 (defun webjump-url-encode (str)
371 (mapconcat '(lambda (c)
372 (cond ((= c 32) "+")
373 ((or (and (>= c ?a) (<= c ?z))
374 (and (>= c ?A) (<= c ?Z))
375 (and (>= c ?0) (<= c ?9)))
376 (char-to-string c))
377 (t (upcase (format "%%%02x" c)))))
378 str
379 ""))
380
381 (defun webjump-url-fix (url)
382 (if (webjump-null-or-blank-string-p url)
383 ""
384 (webjump-url-fix-trailing-slash
385 (cond
386 ((string-match "^[a-zA-Z]+:" url) url)
387 ((string-match "^/" url) (concat "file://" url))
388 ((string-match "^\\([^\\./]+\\)" url)
389 (concat (if (string= (downcase (match-string 1 url)) "ftp")
390 "ftp"
391 "http")
392 "://"
393 url))
394 (t url)))))
395
396 (defun webjump-url-fix-trailing-slash (url)
397 (if (string-match "^[a-zA-Z]+://[^/]+$" url)
398 (concat url "/")
399 url))
400
401 ;;-----------------------------------------------------------------------------
402
403 (provide 'webjump)
404
405 ;;; arch-tag: f1d20156-0a6f-488b-bd91-f69ee8b6d5cc
406 ;;; webjump.el ends here