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