new version
[emacs.git] / lisp / webjump.el
bloba2548da47e6c84f4c00f0d279a515a8cca6d9894
1 ;;; webjump.el --- programmable Web hotlist
3 ;; Copyright (C) 1996 Free Software Foundation
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/
10 ;; This file is part of GNU Emacs.
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.
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.
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.
27 ;;; Commentary:
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.
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.)
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.
44 ;; You may wish to add something like the following to your `.emacs' file:
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))
55 ;; The above loads this package, binds `C-c j' to invoke WebJump, and adds
56 ;; your personal favorite sites to the hotlist.
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.
68 ;; The `browse-url' package is used to submit URLs to the browser, so any
69 ;; browser-specific configuration should be done there.
71 ;; WebJump inherits a small amount code from my `altavista.el' package, and is
72 ;; intended to obsolete that package.
74 ;;; Code:
76 ;;-------------------------------------------------------- Package Dependencies
78 (require 'browse-url)
80 ;;------------------------------------------------------------------- Constants
82 (defvar 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 ("Computer Professionals for Social Responsibility" . "www.cpsr.org/dox/")
93 ("Electronic Frontier Foundation" . "www.eff.org")
94 ("Emacs Lisp Archive" .
95 "ftp://archive.cis.ohio-state.edu/pub/gnu/emacs/elisp-archive/")
96 ("GNU Project" . "www.gnu.ai.mit.edu")
97 ("GNU FTP Archive". [mirrors
98 "ftp://prep.ai.mit.edu/pub/gnu/"
99 ;; ASIA:
100 "ftp://ftp.cs.titech.ac.jp"
101 "ftp://tron.um.u-tokyo.ac.jp/pub/GNU/prep"
102 "ftp://cair-archive.kaist.ac.kr/pub/gnu"
103 "ftp://ftp.nectec.or.th/pub/mirrors/gnu"
104 ;; AUSTRALIA:
105 "ftp://archie.au/gnu"
106 "ftp://archie.oz/gnu"
107 "ftp://archie.oz.au/gnu"
108 ;; AFRICA:
109 "ftp://ftp.sun.ac.za/pub/gnu"
110 ;; MIDDLE-EAST:
111 "ftp://ftp.technion.ac.il/pub/unsupported/gnu"
112 ;; EUROPE:
113 "ftp://irisa.irisa.fr/pub/gnu"
114 "ftp://ftp.univ-lyon1.fr/pub/gnu"
115 "ftp://ftp.mcc.ac.uk"
116 "ftp://unix.hensa.ac.uk/mirrors/uunet/systems/gnu"
117 "ftp://src.doc.ic.ac.uk/gnu"
118 "ftp://ftp.ieunet.ie/pub/gnu"
119 "ftp://ftp.eunet.ch"
120 "ftp://nic.switch.ch/mirror/gnu"
121 "ftp://ftp.informatik.rwth-aachen.de/pub/gnu"
122 "ftp://ftp.informatik.tu-muenchen.de"
123 "ftp://ftp.win.tue.nl/pub/gnu"
124 "ftp://ftp.nl.net"
125 "ftp://ftp.etsimo.uniovi.es/pub/gnu"
126 "ftp://ftp.funet.fi/pub/gnu"
127 "ftp://ftp.denet.dk"
128 "ftp://ftp.stacken.kth.se"
129 "ftp://isy.liu.se"
130 "ftp://ftp.luth.se/pub/unix/gnu"
131 "ftp://ftp.sunet.se/pub/gnu"
132 "ftp://archive.eu.net"
133 ;; SOUTH AMERICA:
134 "ftp://ftp.inf.utfsm.cl/pub/gnu"
135 "ftp://ftp.unicamp.br/pub/gnu"
136 ;; WESTERN CANADA:
137 "ftp://ftp.cs.ubc.ca/mirror2/gnu"
138 ;; USA:
139 "ftp://wuarchive.wustl.edu/systems/gnu"
140 "ftp://labrea.stanford.edu"
141 "ftp://ftp.digex.net/pub/gnu"
142 "ftp://ftp.kpc.com/pub/mirror/gnu"
143 "ftp://f.ms.uky.edu/pub3/gnu"
144 "ftp://jaguar.utah.edu/gnustuff"
145 "ftp://ftp.hawaii.edu/mirrors/gnu"
146 "ftp://uiarchive.cso.uiuc.edu/pub/gnu"
147 "ftp://ftp.cs.columbia.edu/archives/gnu/prep"
148 "ftp://gatekeeper.dec.com/pub/GNU"
149 "ftp://ftp.uu.net/systems/gnu"
151 ("Interactive Weather Information Network" . webjump-to-iwin)
152 ("Lycos" .
153 [simple-query "www.lycos.com" "www.lycos.com/cgi-bin/pursuit?query=" ""])
154 ("Usenet FAQs" .
155 [simple-query "www.cis.ohio-state.edu/hypertext/faq/usenet/FAQ-List.html"
156 "www.cis.ohio-state.edu/htbin/search-usenet-faqs/form?find="
157 ""])
158 ("Risks Digest" . webjump-to-risks)
159 ("RTFM Usenet FAQs by Group" .
160 "ftp://rtfm.mit.edu/pub/usenet-by-group/")
161 ("RTFM Usenet FAQs by Hierachy" .
162 "ftp://rtfm.mit.edu/pub/usenet-by-hierarchy/")
163 ("X Consortium Archive". "ftp.x.org")
164 ("Yahoo" .
165 [simple-query "www.yahoo.com" "search.yahoo.com/bin/search?p=" ""])
166 ("Yahoo Reference" "www.yahoo.com/Reference/")
168 "Sample hotlist for WebJump.")
170 (defvar webjump-state-to-postal-alist
171 '(("Alabama" . "al") ("Alaska" . "ak") ("Arizona" . "az") ("Arkansas" . "ar")
172 ("California" . "ca") ("Colorado" . "co") ("Connecticut" . "ct")
173 ("Delaware" . "de") ("Florida" . "fl") ("Georgia" . "ga") ("Hawaii" . "hi")
174 ("Idaho" . "id") ("Illinois" . "il") ("Indiana" . "in") ("Iowa" . "ia")
175 ("Kansas" . "ks") ("Kentucky" . "ky") ("Louisiana" . "la") ("Maine" . "me")
176 ("Maryland" . "md") ("Massachusetts" . "ma") ("Michigan" . "mi")
177 ("Minnesota" . "mn") ("Mississippi" . "ms") ("Missouri" . "mo")
178 ("Montana" . "mt") ("Nebraska" . "ne") ("Nevada" . "nv")
179 ("New Hampshire" . "nh") ("New Jersey" . "nj") ("New Mexico" . "nm")
180 ("New York" . "ny") ("North Carolina" . "nc") ("North Dakota" . "nd")
181 ("Ohio" . "oh") ("Oklahoma" . "ok") ("Oregon" . "or")
182 ("Pennsylvania" . "pa") ("Rhode Island" . "ri") ("South Carolina" . "sc")
183 ("South Dakota" . "sd") ("Tennessee" . "tn") ("Texas" . "tx")
184 ("Utah" . "ut") ("Vermont" . "vt") ("Virginia" . "va")
185 ("Washington" . "wa") ("West Virginia" . "wv") ("Wisconsin" . "wi")
186 ("Wyoming" . "wy")))
188 ;;------------------------------------------------------------ Option Variables
190 (defvar webjump-sites
191 webjump-sample-sites
192 "*Hotlist for WebJump.
194 The hotlist is represented as an association list, with the CAR of each cell
195 being the name of the Web site, and the CDR being the definition for the URL of
196 that site. The URL definition can be a string (the URL), a vector (specifying
197 a special \"builtin\" which returns a URL), a symbol (name of a function which
198 returns a URL), or a list (which when `eval'ed yields a URL).
200 If the URL definition is a vector, then a \"builtin\" is used. A builtin has a
201 Lisp-like syntax, with the name as the first element of the vector, and any
202 arguments as the following elements. The three current builtins are `name',
203 which returns the name of the site as the URL, `simple-query', which
204 returns a URL that is a function of a query entered by the user, and `mirrors',
205 which allows the user to select from among multiple mirror sites for the same
206 content.
208 The first argument to the `simple-query' builtin is a static URL to use if the
209 user enters a blank query. The second and third arguments are the prefix and
210 suffix, respectively, to add to the encoded query the user enters. This
211 builtin covers Web sites that have single-string searches with the query
212 embedded in the URL.
214 The arguments to the `mirrors' builtin are URLs of mirror sites.
216 If the symbol of a function is given, then the function will be called with the
217 Web site name (the one you specified in the CAR of the alist cell) as a
218 parameter. This might come in handy for various kludges.
220 For convenience, if the `http://', `ftp://', or `file://' prefix is missing
221 from a URL, WebJump will make a guess at what you wanted and prepend it before
222 submitting the URL.")
224 ;;------------------------------------------------------- Sample Site Functions
226 (defun webjump-to-iwin (name)
227 (let ((prefix "http://iwin.nws.noaa.gov/")
228 (state (webjump-read-choice name "state"
229 (append '(("Puerto Rico" . "pr"))
230 webjump-state-to-postal-alist))))
231 (if state
232 (concat prefix "iwin/" state "/"
233 (webjump-read-choice name "option"
234 '(("Hourly Report" . "hourly")
235 ("State Forecast" . "state")
236 ("Local Forecast" . "local")
237 ("Zone Forecast" . "zone")
238 ("Short-Term Forecast" . "shortterm")
239 ("Weather Summary" . "summary")
240 ("Public Information" . "public")
241 ("Climatic Data" . "climate")
242 ("Aviation Products" . "aviation")
243 ("Hydro Products" . "hydro")
244 ("Special Weather" . "special")
245 ("Watches and Warnings" . "warnings"))
246 "zone")
247 ".html")
248 prefix)))
250 (defun webjump-to-risks (name)
251 (let (issue volume)
252 (if (and (setq volume (webjump-read-number (concat name " volume")))
253 (setq issue (webjump-read-number (concat name " issue"))))
254 (format "catless.ncl.ac.uk/Risks/%d.%02d.html" volume issue)
255 "catless.ncl.ac.uk/Risks/")))
257 ;;-------------------------------------------------------------- Core Functions
259 ;;;###autoload
260 (defun webjump ()
261 "Jumps to a Web site from a programmable hotlist.
263 See the documentation for the `webjump-sites' variable for how to customize the
264 hotlist.
266 Feedback on WebJump can be sent to the author, Neil W. Van Dyke <nwv@acm.org>,
267 or submitted via `\\[webjump-submit-bug-report]'. The latest version can be
268 gotten from `http://www.cs.brown.edu/people/nwv/'."
269 (interactive)
270 (let* ((completion-ignore-case t)
271 (item (assoc (completing-read "WebJump to site: " webjump-sites nil t)
272 webjump-sites))
273 (name (car item))
274 (expr (cdr item)))
275 (funcall browse-url-browser-function
276 (webjump-url-fix
277 (cond ((not expr) "")
278 ((stringp expr) expr)
279 ((vectorp expr) (webjump-builtin expr name))
280 ((listp expr) (eval expr))
281 ((symbolp expr)
282 (if (fboundp expr)
283 (funcall expr name)
284 (error "WebJump URL function \"%s\" undefined." expr)))
285 (t (error "WebJump URL expression for \"%s\" invalid."
286 name)))))))
288 (defun webjump-builtin (expr name)
289 (if (< (length expr) 1)
290 (error "WebJump URL builtin for \"%s\" empty." name))
291 (let ((builtin (aref expr 0)))
292 (cond
293 ((eq builtin 'mirrors)
294 (if (= (length expr) 1)
295 (error
296 "WebJump URL builtin \"mirrors\" for \"%s\" needs at least 1 arg."))
297 (webjump-choose-mirror name (cdr (append expr nil))))
298 ((eq builtin 'name)
299 name)
300 ((eq builtin 'simple-query)
301 (webjump-builtin-check-args expr name 3)
302 (webjump-do-simple-query name (aref expr 1) (aref expr 2) (aref expr 3)))
303 (t (error "WebJump URL builtin \"%s\" for \"%s\" invalid."
304 builtin name)))))
306 (defun webjump-builtin-check-args (expr name count)
307 (or (= (length expr) (1+ count))
308 (error "WebJump URL builtin \"%s\" for \"%s\" needs %d args."
309 (aref expr 0) name count)))
311 (defun webjump-choose-mirror (name urls)
312 (webjump-read-url-choice (concat name " mirror")
313 urls
314 (webjump-mirror-default urls)))
316 (defun webjump-do-simple-query (name noquery-url query-prefix query-suffix)
317 (let ((query (webjump-read-string (concat name " query"))))
318 (if query
319 (concat query-prefix (webjump-url-encode query) query-suffix)
320 noquery-url)))
322 (defun webjump-mirror-default (urls)
323 ;; Note: This should be modified to apply some simple kludges/heuristics to
324 ;; pick a site which is likely "close". As a tie-breaker among candidates
325 ;; judged equally desirable, randomness should be used.
326 (car urls))
328 (defun webjump-read-choice (name what choices &optional default)
329 (let* ((completion-ignore-case t)
330 (choice (completing-read (concat name " " what ": ") choices nil t)))
331 (if (webjump-null-or-blank-string-p choice)
332 default
333 (cdr (assoc choice choices)))))
335 (defun webjump-read-number (prompt)
336 ;; Note: I should make this more robust someday.
337 (let ((input (webjump-read-string prompt)))
338 (if input (string-to-number input))))
340 (defun webjump-read-string (prompt)
341 (let ((input (read-string (concat prompt ": "))))
342 (if (webjump-null-or-blank-string-p input) nil input)))
344 (defun webjump-read-url-choice (what urls &optional default)
345 ;; Note: Convert this to use `webjump-read-choice' someday.
346 (let* ((completions (mapcar (function (lambda (n) (cons n n)))
347 urls))
348 (input (completing-read (concat what
349 ;;(if default " (RET for default)" "")
350 ": ")
351 completions
353 t)))
354 (if (webjump-null-or-blank-string-p input)
355 default
356 (car (assoc input completions)))))
358 (defun webjump-null-or-blank-string-p (str)
359 (or (null str) (string-match "^[ \t]*$" str)))
361 (defun webjump-url-encode (str)
362 (mapconcat '(lambda (c)
363 (cond ((= c 32) "+")
364 ((or (and (>= c ?a) (<= c ?z))
365 (and (>= c ?A) (<= c ?Z))
366 (and (>= c ?0) (<= c ?9)))
367 (char-to-string c))
368 (t (upcase (format "%%%02x" c)))))
370 ""))
372 (defun webjump-url-fix (url)
373 (if (webjump-null-or-blank-string-p url)
375 (webjump-url-fix-trailing-slash
376 (cond
377 ((string-match "^[a-zA-Z]+:" url) url)
378 ((string-match "^/" url) (concat "file://" url))
379 ((string-match "^\\([^\\./]+\\)" url)
380 (concat (if (string= (downcase (match-string 1 url)) "ftp")
381 "ftp"
382 "http")
383 "://"
384 url))
385 (t url)))))
387 (defun webjump-url-fix-trailing-slash (url)
388 (if (string-match "^[a-zA-Z]+://[^/]+$" url)
389 (concat url "/")
390 url))
392 ;;-----------------------------------------------------------------------------
394 (provide 'webjump)
396 ;; webjump.el ends here