Initial revision
[bpt/emacs.git] / lisp / webjump.el
1 ;;; webjump.el --- programmable Web hotlist
2
3 ;; Copyright (C) 1996 Free Software Foundation
4
5 ;; Author: Neil W. Van Dyke <nwv@acm.org>
6 ;; Created: Fri 09 Aug 1996
7 ;; Keywords: comm www
8 ;; X-URL: http://www.cs.brown.edu/people/nwv/
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., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, 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 ;; (load "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
56 ;; your personal favorite sites to the hotlist.
57
58 ;; The `webjump-sample-sites' constant mostly contains sites that are expected
59 ;; to be generally useful to Emacs users or that have some sort of query which
60 ;; can be coded in WebJump. There are two main goals of this sample site list:
61 ;; (1) demonstrate WebJump capabilities and usage; (2) provide definitions for
62 ;; many popular sites so that people don't have to reinvent the wheel. A few
63 ;; assorted other sites have been thrown in on a whim. No commercial sites are
64 ;; included unless they provide a free, generally-useful service. Inclusion of
65 ;; a site does not represent an endorsement. Please contact the maintainer
66 ;; with change requests.
67
68 ;; The `browse-url' package is used to submit URLs to the browser, so any
69 ;; browser-specific configuration should be done there.
70
71 ;; WebJump inherits a small amount code from my `altavista.el' package, and is
72 ;; intended to obsolete that package.
73
74 ;;; Code:
75
76 ;;-------------------------------------------------------- Package Dependencies
77
78 (require 'browse-url)
79
80 ;;------------------------------------------------------------------- Constants
81
82 (defconst-TEST webjump-sample-sites
83 '(("AltaVista" .
84 [simple-query
85 "www.altavista.digital.com"
86 "www.altavista.digital.com/cgi-bin/query?pg=aq&what=web&fmt=.&q="
87 "&r=&d0=&d1="])
88 ("Archie" .
89 [simple-query "http://hoohoo.ncsa.uiuc.edu/cgi-bin/AA.pl"
90 "http://hoohoo.ncsa.uiuc.edu/cgi-bin/AA.pl?query=" ""])
91 ("Bastard Operator from Hell" . "www.replay.com/bofh/")
92 ("Brown University" .
93 [simple-query "www.brown.edu" "www.brown.edu/cgi-local/bsearch?" ""])
94 ("Computer Professionals for Social Responsibility" . "www.cpsr.org/dox/")
95 ("Digital Espresso" .
96 [simple-query "www.io.org/~mentor/DigitalEspresso.html"
97 "www.jars.com/cgi-bin/aglimpse/01?query="
98 "&case=on&whole=on&errors=0&maxfiles=100&maxlines=30"])
99 ("Dilbert" . "www.unitedmedia.com/comics/dilbert/")
100 ("Electronic Frontier Foundation" . "www.eff.org")
101 ("Emacs Lisp Archive" .
102 "ftp://archive.cis.ohio-state.edu/pub/gnu/emacs/elisp-archive/")
103 ("Free Software Foundation" . "www.fsf.org")
104 ("GNU FTP Archive". [mirrors
105 "ftp://prep.ai.mit.edu/pub/gnu/"
106 ;; ASIA:
107 "ftp://ftp.cs.titech.ac.jp"
108 "ftp://tron.um.u-tokyo.ac.jp/pub/GNU/prep"
109 "ftp://cair-archive.kaist.ac.kr/pub/gnu"
110 "ftp://ftp.nectec.or.th/pub/mirrors/gnu"
111 ;; AUSTRALIA:
112 "ftp://archie.au/gnu"
113 "ftp://archie.oz/gnu"
114 "ftp://archie.oz.au/gnu"
115 ;; AFRICA:
116 "ftp://ftp.sun.ac.za/pub/gnu"
117 ;; MIDDLE-EAST:
118 "ftp://ftp.technion.ac.il/pub/unsupported/gnu"
119 ;; EUROPE:
120 "ftp://irisa.irisa.fr/pub/gnu"
121 "ftp://ftp.univ-lyon1.fr/pub/gnu"
122 "ftp://ftp.mcc.ac.uk"
123 "ftp://unix.hensa.ac.uk/mirrors/uunet/systems/gnu"
124 "ftp://src.doc.ic.ac.uk/gnu"
125 "ftp://ftp.ieunet.ie/pub/gnu"
126 "ftp://ftp.eunet.ch"
127 "ftp://nic.switch.ch/mirror/gnu"
128 "ftp://ftp.informatik.rwth-aachen.de/pub/gnu"
129 "ftp://ftp.informatik.tu-muenchen.de"
130 "ftp://ftp.win.tue.nl/pub/gnu"
131 "ftp://ftp.nl.net"
132 "ftp://ftp.etsimo.uniovi.es/pub/gnu"
133 "ftp://ftp.funet.fi/pub/gnu"
134 "ftp://ftp.denet.dk"
135 "ftp://ftp.stacken.kth.se"
136 "ftp://isy.liu.se"
137 "ftp://ftp.luth.se/pub/unix/gnu"
138 "ftp://ftp.sunet.se/pub/gnu"
139 "ftp://archive.eu.net"
140 ;; SOUTH AMERICA:
141 "ftp://ftp.inf.utfsm.cl/pub/gnu"
142 "ftp://ftp.unicamp.br/pub/gnu"
143 ;; WESTERN CANADA:
144 "ftp://ftp.cs.ubc.ca/mirror2/gnu"
145 ;; USA:
146 "ftp://wuarchive.wustl.edu/systems/gnu"
147 "ftp://labrea.stanford.edu"
148 "ftp://ftp.digex.net/pub/gnu"
149 "ftp://ftp.kpc.com/pub/mirror/gnu"
150 "ftp://f.ms.uky.edu/pub3/gnu"
151 "ftp://jaguar.utah.edu/gnustuff"
152 "ftp://ftp.hawaii.edu/mirrors/gnu"
153 "ftp://uiarchive.cso.uiuc.edu/pub/gnu"
154 "ftp://ftp.cs.columbia.edu/archives/gnu/prep"
155 "ftp://gatekeeper.dec.com/pub/GNU"
156 "ftp://ftp.uu.net/systems/gnu"
157 ])
158 ("Insidious Big Brother Database" . "home.netscape.com/people/jwz/bbdb/")
159 ("Interactive Weather Information Network" . webjump-to-iwin)
160 ("Java API" . webjump-to-javaapi)
161 ("Lycos" .
162 [simple-query "www.lycos.com" "www.lycos.com/cgi-bin/pursuit?query=" ""])
163 ("Mailcrypt" . "cag-www.lcs.mit.edu/mailcrypt/")
164 ("Pretty Good Privacy" . "web.mit.edu/network/pgp.html")
165 ("Playboy" . (if (webjump-adult-p) "www.playboy.com" "www.whitehouse.gov"))
166 ("Usenet FAQs" .
167 [simple-query "www.cis.ohio-state.edu/hypertext/faq/usenet/FAQ-List.html"
168 "www.cis.ohio-state.edu/htbin/search-usenet-faqs/form?find="
169 ""])
170 ("Risks Digest" . webjump-to-risks)
171 ("RTFM Usenet FAQs by Group" .
172 "ftp://rtfm.mit.edu/pub/usenet-by-group/")
173 ("RTFM Usenet FAQs by Hierachy" .
174 "ftp://rtfm.mit.edu/pub/usenet-by-hierarchy/")
175 ("Webster" .
176 [simple-query "c.gp.cs.cmu.edu:5103/prog/webster"
177 "gs213.sp.cs.cmu.edu/prog/webster?" ""])
178 ("X Consortium Archive". "ftp.x.org")
179 ("Yahoo" .
180 [simple-query "www.yahoo.com" "search.yahoo.com/bin/search?p=" ""])
181 ("Yahoo Emacs" .
182 "www.yahoo.com/Computers_and_Internet/Software/Editors/Emacs/")
183 ("Yahoo Reference" "www.yahoo.com/Reference/")
184 )
185 "Sample hotlist for WebJump.")
186
187 (defconst webjump-state-to-postal-alist
188 '(("Alabama" . "al") ("Alaska" . "ak") ("Arizona" . "az") ("Arkansas" . "ar")
189 ("California" . "ca") ("Colorado" . "co") ("Connecticut" . "ct")
190 ("Delaware" . "de") ("Florida" . "fl") ("Georgia" . "ga") ("Hawaii" . "hi")
191 ("Idaho" . "id") ("Illinois" . "il") ("Indiana" . "in") ("Iowa" . "ia")
192 ("Kansas" . "ks") ("Kentucky" . "ky") ("Louisiana" . "la") ("Maine" . "me")
193 ("Maryland" . "md") ("Massachusetts" . "ma") ("Michigan" . "mi")
194 ("Minnesota" . "mn") ("Mississippi" . "ms") ("Missouri" . "mo")
195 ("Montana" . "mt") ("Nebraska" . "ne") ("Nevada" . "nv")
196 ("New Hampshire" . "nh") ("New Jersey" . "nj") ("New Mexico" . "nm")
197 ("New York" . "ny") ("North Carolina" . "nc") ("North Dakota" . "nd")
198 ("Ohio" . "oh") ("Oklahoma" . "ok") ("Oregon" . "or")
199 ("Pennsylvania" . "pa") ("Rhode Island" . "ri") ("South Carolina" . "sc")
200 ("South Dakota" . "sd") ("Tennessee" . "tn") ("Texas" . "tx")
201 ("Utah" . "ut") ("Vermont" . "vt") ("Virginia" . "va")
202 ("Washington" . "wa") ("West Virginia" . "wv") ("Wisconsin" . "wi")
203 ("Wyoming" . "wy")))
204
205 ;;------------------------------------------------------------ Option Variables
206
207 (defvar webjump-sites
208 webjump-sample-sites
209 "*Hotlist for WebJump.
210
211 The hotlist is represented as an association list, with the CAR of each cell
212 being the name of the Web site, and the CDR being the definition for the URL of
213 that site. The URL definition can be a string (the URL), a vector (specifying
214 a special \"builtin\" which returns a URL), a symbol (name of a function which
215 returns a URL), or a list (which when `eval'ed yields a URL).
216
217 If the URL definition is a vector, then a \"builtin\" is used. A builtin has a
218 Lisp-like syntax, with the name as the first element of the vector, and any
219 arguments as the following elements. The three current builtins are `name',
220 which returns the name of the site as the URL, `simple-query', which
221 returns a URL that is a function of a query entered by the user, and `mirrors',
222 which allows the user to select from among multiple mirror sites for the same
223 content.
224
225 The first argument to the `simple-query' builtin is a static URL to use if the
226 user enters a blank query. The second and third arguments are the prefix and
227 suffix, respectively, to add to the encoded query the user enters. This
228 builtin covers Web sites that have single-string searches with the query
229 embedded in the URL.
230
231 The arguments to the `mirrors' builtin are URLs of mirror sites.
232
233 If the symbol of a function is given, then the function will be called with the
234 Web site name (the one you specified in the CAR of the alist cell) as a
235 parameter. This might come in handy for various kludges.
236
237 For convenience, if the `http://', `ftp://', or `file://' prefix is missing
238 from a URL, WebJump will make a guess at what you wanted and prepend it before
239 submitting the URL.")
240
241 ;;------------------------------------------------------- Sample Site Functions
242
243 (defun webjump-to-iwin (name)
244 (let ((prefix "http://iwin.nws.noaa.gov/")
245 (state (webjump-read-choice name "state"
246 (append '(("Puerto Rico" . "pr"))
247 webjump-state-to-postal-alist))))
248 (if state
249 (concat prefix "iwin/" state "/"
250 (webjump-read-choice name "option"
251 '(("Hourly Report" . "hourly")
252 ("State Forecast" . "state")
253 ("Local Forecast" . "local")
254 ("Zone Forecast" . "zone")
255 ("Short-Term Forecast" . "shortterm")
256 ("Weather Summary" . "summary")
257 ("Public Information" . "public")
258 ("Climatic Data" . "climate")
259 ("Aviation Products" . "aviation")
260 ("Hydro Products" . "hydro")
261 ("Special Weather" . "special")
262 ("Watches and Warnings" . "warnings"))
263 "zone")
264 ".html")
265 prefix)))
266
267 (defun webjump-to-javaapi (name)
268 (let* ((prefix "http://www.javasoft.com/products/JDK/CurrentRelease/api/")
269 (packages '(("java.applet") ("java.awt") ("java.awt.image")
270 ("java.awt.peer") ("java.io") ("java.lang") ("java.net")
271 ("java.util") ("sun.tools.debug")))
272 (completion-ignore-case t)
273 (package (completing-read (concat name " package: ") packages nil t)))
274 (if (webjump-null-or-blank-string-p package)
275 (concat prefix "packages.html")
276 (concat prefix "Package-" package ".html"))))
277
278 (defun webjump-to-risks (name)
279 (let (issue volume)
280 (if (and (setq volume (webjump-read-number (concat name " volume")))
281 (setq issue (webjump-read-number (concat name " issue"))))
282 (format "catless.ncl.ac.uk/Risks/%d.%02d.html" volume issue)
283 "catless.ncl.ac.uk/Risks/")))
284
285 ;;-------------------------------------------------------------- Core Functions
286
287 ;;;###autoload
288 (defun webjump ()
289 "Jumps to a Web site from a programmable hotlist.
290
291 See the documentation for the `webjump-sites' variable for how to customize the
292 hotlist.
293
294 Feedback on WebJump can be sent to the author, Neil W. Van Dyke <nwv@acm.org>,
295 or submitted via `\\[webjump-submit-bug-report]'. The latest version can be
296 gotten from `http://www.cs.brown.edu/people/nwv/'."
297 (interactive)
298 (let* ((completion-ignore-case t)
299 (item (assoc (completing-read "WebJump to site: " webjump-sites nil t)
300 webjump-sites))
301 (name (car item))
302 (expr (cdr item)))
303 (funcall browse-url-browser-function
304 (webjump-url-fix
305 (cond ((not expr) "")
306 ((stringp expr) expr)
307 ((vectorp expr) (webjump-builtin expr name))
308 ((listp expr) (eval expr))
309 ((symbolp expr)
310 (if (fboundp expr)
311 (funcall expr name)
312 (error "WebJump URL function \"%s\" undefined." expr)))
313 (t (error "WebJump URL expression for \"%s\" invalid."
314 name)))))))
315
316 (defun webjump-adult-p ()
317 (and (boundp 'age) (integerp age) (>= age 21)))
318
319 (defun webjump-builtin (expr name)
320 (if (< (length expr) 1)
321 (error "WebJump URL builtin for \"%s\" empty." name))
322 (let ((builtin (aref expr 0)))
323 (cond
324 ((eq builtin 'mirrors)
325 (if (= (length expr) 1)
326 (error
327 "WebJump URL builtin \"mirrors\" for \"%s\" needs at least 1 arg."))
328 (webjump-choose-mirror name (cdr (append expr nil))))
329 ((eq builtin 'name)
330 name)
331 ((eq builtin 'simple-query)
332 (webjump-builtin-check-args expr name 3)
333 (webjump-do-simple-query name (aref expr 1) (aref expr 2) (aref expr 3)))
334 (t (error "WebJump URL builtin \"%s\" for \"%s\" invalid."
335 builtin name)))))
336
337 (defun webjump-builtin-check-args (expr name count)
338 (or (= (length expr) (1+ count))
339 (error "WebJump URL builtin \"%s\" for \"%s\" needs %d args."
340 (aref expr 0) name count)))
341
342 (defun webjump-choose-mirror (name urls)
343 (webjump-read-url-choice (concat name " mirror")
344 urls
345 (webjump-mirror-default urls)))
346
347 (defun webjump-do-simple-query (name noquery-url query-prefix query-suffix)
348 (let ((query (webjump-read-string (concat name " query"))))
349 (if query
350 (concat query-prefix (webjump-url-encode query) query-suffix)
351 noquery-url)))
352
353 (defun webjump-mirror-default (urls)
354 ;; Note: This should be modified to apply some simple kludges/heuristics to
355 ;; pick a site which is likely "close". As a tie-breaker among candidates
356 ;; judged equally desirable, randomness should be used.
357 (car urls))
358
359 (defun webjump-read-choice (name what choices &optional default)
360 (let* ((completion-ignore-case t)
361 (choice (completing-read (concat name " " what ": ") choices nil t)))
362 (if (webjump-null-or-blank-string-p choice)
363 default
364 (cdr (assoc choice choices)))))
365
366 (defun webjump-read-number (prompt)
367 ;; Note: I should make this more robust someday.
368 (let ((input (webjump-read-string prompt)))
369 (if input (string-to-number input))))
370
371 (defun webjump-read-string (prompt)
372 (let ((input (read-string (concat prompt ": "))))
373 (if (webjump-null-or-blank-string-p input) nil input)))
374
375 (defun webjump-read-url-choice (what urls &optional default)
376 ;; Note: Convert this to use `webjump-read-choice' someday.
377 (let* ((completions (mapcar (function (lambda (n) (cons n n)))
378 urls))
379 (input (completing-read (concat what
380 ;;(if default " (RET for default)" "")
381 ": ")
382 completions
383 nil
384 t)))
385 (if (webjump-null-or-blank-string-p input)
386 default
387 (car (assoc input completions)))))
388
389 (defun webjump-null-or-blank-string-p (str)
390 (or (null str) (string-match "^[ \t]*$" str)))
391
392 (defun webjump-url-encode (str)
393 (mapconcat '(lambda (c)
394 (cond ((= c 32) "+")
395 ((or (and (>= c ?a) (<= c ?z))
396 (and (>= c ?A) (<= c ?Z))
397 (and (>= c ?0) (<= c ?9)))
398 (char-to-string c))
399 (t (upcase (format "%%%02x" c)))))
400 str
401 ""))
402
403 (defun webjump-url-fix (url)
404 (if (webjump-null-or-blank-string-p url)
405 ""
406 (webjump-url-fix-trailing-slash
407 (cond
408 ((string-match "^[a-zA-Z]+:" url) url)
409 ((string-match "^/" url) (concat "file://" url))
410 ((string-match "^\\([^\\./]+\\)" url)
411 (concat (if (string= (downcase (match-string 1 url)) "ftp")
412 "ftp"
413 "http")
414 "://"
415 url))
416 (t url)))))
417
418 (defun webjump-url-fix-trailing-slash (url)
419 (if (string-match "^[a-zA-Z]+://[^/]+$" url)
420 (concat url "/")
421 url))
422
423 ;;-----------------------------------------------------------------------------
424
425 (provide 'webjump)
426
427 ;; webjump.el ends here