]> code.delx.au - gnu-emacs/blob - lisp/net/webjump.el
6ef24e9f3549c4f014b18e8a4f1267698804283a
[gnu-emacs] / lisp / net / webjump.el
1 ;;; webjump.el --- programmable Web hotlist
2
3 ;; Copyright (C) 1996-1997, 2001-2011 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 3 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; WebJump provides a sort of ``programmable hotlist'' of Web sites that can
27 ;; quickly be invoked in your Web browser. Each Web site in the hotlist has a
28 ;; name, and you select the desired site name via a completing string prompt in
29 ;; the minibuffer. The URL for each Web site is defined as a static string or
30 ;; a built-in or custom function, allowing interactive prompting for
31 ;; site-specific queries and options.
32
33 ;; Note that WebJump was originally intended to complement your conventional
34 ;; browser-based hotlist, not replace it. (Though there's no reason you
35 ;; couldn't use WebJump for your entire hotlist if you were so inclined.)
36
37 ;; The `webjump-sites' variable, which defines the hotlist, defaults to some
38 ;; example sites. You'll probably want to override it with your own favorite
39 ;; sites. The documentation for the variable describes the syntax.
40
41 ;; You may wish to add something like the following to your `.emacs' file:
42 ;;
43 ;; (require 'webjump)
44 ;; (global-set-key "\C-cj" 'webjump)
45 ;; (setq webjump-sites
46 ;; (append '(
47 ;; ("My Home Page" . "www.someisp.net/users/joebobjr/")
48 ;; ("Pop's Site" . "www.joebob-and-son.com/")
49 ;; )
50 ;; webjump-sample-sites))
51 ;;
52 ;; The above loads this package, binds `C-c j' to invoke WebJump, and adds your
53 ;; personal favorite sites to the hotlist.
54
55 ;; The `webjump-sample-sites' variable mostly contains some site entries that
56 ;; are expected to be generally relevant to many users, but excluding
57 ;; those that the GNU project would not want to recommend.
58
59 ;; The `browse-url' package is used to submit URLs to the browser, so any
60 ;; browser-specific configuration should be done there.
61
62 ;;; Code:
63
64 ;;-------------------------------------------------------- Package Dependencies
65
66 (require 'browse-url)
67
68 ;;------------------------------------------------------------------- Constants
69
70 (defvar webjump-sample-sites
71 '(
72 ;; FSF, not including Emacs-specific.
73 ("GNU Project FTP Archive" .
74 ;; GNU FTP Mirror List from http://www.gnu.org/order/ftp.html
75 [mirrors "ftp://ftp.gnu.org/pub/gnu/"
76 ;; United States
77 "ftp://mirrors.kernel.org/gnu"
78 "ftp://gatekeeper.dec.com/pub/GNU/"
79 "ftp://ftp.keystealth.org/pub/gnu/"
80 "ftp://mirrors.usc.edu/pub/gnu/"
81 "ftp://cudlug.cudenver.edu/pub/mirrors/ftp.gnu.org/"
82 "ftp://ftp.cise.ufl.edu/pub/mirrors/GNU/"
83 "ftp://uiarchive.cso.uiuc.edu/pub/ftp/ftp.gnu.org/gnu/"
84 "ftp://gnu.cs.lewisu.edu/gnu/"
85 "ftp://ftp.in-span.net/pub/mirrors/ftp.gnu.org/"
86 "ftp://gnu.ms.uky.edu/pub/mirrors/gnu/"
87 "ftp://ftp.algx.net/pub/gnu/"
88 "ftp://aeneas.mit.edu/pub/gnu/"
89 "ftp://ftp.egr.msu.edu/pub/gnu/"
90 "ftp://ftp.wayne.edu/pub/gnu/"
91 "ftp://wuarchive.wustl.edu/mirrors/gnu/"
92 "ftp://gnu.teleglobe.net/ftp.gnu.org/"
93 "ftp://ftp.cs.columbia.edu/archives/gnu/prep/"
94 "ftp://ftp.ece.cornell.edu/pub/mirrors/gnu/"
95 "ftp://ftp.ibiblio.org/pub/mirrors/gnu/"
96 "ftp://ftp.cis.ohio-state.edu/mirror/gnu/"
97 "ftp://ftp.club.cc.cmu.edu/gnu/"
98 "ftp://ftp.sunsite.utk.edu/pub/gnu/ftp/"
99 "ftp://thales.memphis.edu/pub/gnu/"
100 "ftp://gnu.wwc.edu"
101 "ftp://ftp.twtelecom.net/pub/GNU/"
102 ;; Africa
103 "ftp://ftp.sun.ac.za/mirrorsites/ftp.gnu.org"
104 ;; The Americas
105 "ftp://ftp.unicamp.br/pub/gnu/"
106 "ftp://master.softaplic.com.br/pub/gnu/"
107 "ftp://ftp.matrix.com.br/pub/gnu/"
108 "ftp://ftp.pucpr.br/gnu"
109 "ftp://ftp.linorg.usp.br/gnu"
110 "ftp://ftp.cs.ubc.ca/mirror2/gnu/"
111 "ftp://cs.ubishops.ca/pub/ftp.gnu.org/"
112 "ftp://ftp.inf.utfsm.cl/pub/gnu/"
113 "ftp://sunsite.ulatina.ac.cr/Mirrors/GNU/"
114 "ftp://www.gnu.unam.mx/pub/gnu/software/"
115 "ftp://gnu.cem.itesm.mx/pub/mirrors/gnu.org/"
116 "ftp://ftp.azc.uam.mx/mirrors/gnu/"
117 ;; Australia
118 "ftp://mirror.aarnet.edu.au/pub/gnu/"
119 "ftp://gnu.mirror.pacific.net.au/gnu/"
120 ;; Asia
121 "ftp://ftp.cs.cuhk.edu.hk/pub/gnu/gnu/"
122 "ftp://sunsite.ust.hk/pub/gnu/"
123 "ftp://ftp.gnupilgrims.org/pub/gnu"
124 "ftp://www.imtech.res.in/mirror/gnuftp/"
125 "ftp://kambing.vlsm.org/gnu"
126 "ftp://ftp.cs.huji.ac.il/mirror/GNU/"
127 "ftp://tron.um.u-tokyo.ac.jp/pub/GNU/"
128 "ftp://core.ring.gr.jp/pub/GNU/"
129 "ftp://ftp.ring.gr.jp/pub/GNU/"
130 "ftp://mirrors.hbi.co.jp/gnu/"
131 "ftp://ftp.cs.titech.ac.jp/pub/gnu/"
132 "ftp://ftpmirror.hanyang.ac.kr/GNU/"
133 "ftp://ftp.linux.sarang.net/mirror/gnu/gnu/"
134 "ftp://ftp.xgate.co.kr/pub/mirror/gnu/"
135 "ftp://ftp://gnu.xinicks.com/"
136 "ftp://ftp.isu.net.sa/pub/gnu/"
137 "ftp://ftp.nctu.edu.tw/UNIX/gnu/"
138 "ftp://coda.nctu.edu.tw/UNIX/gnu/"
139 "ftp://ftp1.sinica.edu.tw/pub3/GNU/gnu/"
140 "ftp://gnu.cdpa.nsysu.edu.tw/gnu"
141 "ftp://ftp.nectec.or.th/pub/mirrors/gnu/"
142 ;; Europe
143 "ftp://ftp.gnu.vbs.at/"
144 "ftp://ftp.univie.ac.at/packages/gnu/"
145 "ftp://gd.tuwien.ac.at/gnu/gnusrc/"
146 "ftp://ftp.belnet.be/mirror/ftp.gnu.org/"
147 "ftp://gnu.blic.net/pub/gnu/"
148 "ftp://ftp.fi.muni.cz/pub/gnu/"
149 "ftp://ftp.dkuug.dk/pub/gnu/"
150 "ftp://sunsite.dk/mirrors/gnu"
151 "ftp://ftp.funet.fi/pub/gnu/prep/"
152 "ftp://ftp.irisa.fr/pub/gnu/"
153 "ftp://ftp.cs.univ-paris8.fr/mirrors/ftp.gnu.org/"
154 "ftp://ftp.cs.tu-berlin.de/pub/gnu/"
155 "ftp://ftp.leo.org/pub/comp/os/unix/gnu/"
156 "ftp://ftp.informatik.rwth-aachen.de/pub/gnu/"
157 "ftp://ftp.de.uu.net/pub/gnu/"
158 "ftp://ftp.freenet.de/pub/ftp.gnu.org/gnu/"
159 "ftp://ftp.cs.uni-bonn.de/pub/gnu/"
160 "ftp://ftp-stud.fht-esslingen.de/pub/Mirrors/ftp.gnu.org/"
161 "ftp://ftp.stw-bonn.de/pub/mirror/ftp.gnu.org/"
162 "ftp://ftp.math.uni-bremen.de/pub/gnu"
163 "ftp://ftp.forthnet.gr/pub/gnu/"
164 "ftp://ftp.ntua.gr/pub/gnu/"
165 "ftp://ftp.duth.gr/pub/gnu/"
166 "ftp://ftp.physics.auth.gr/pub/gnu/"
167 "ftp://ftp.esat.net/pub/gnu/"
168 "ftp://ftp.heanet.ie/mirrors/ftp.gnu.org"
169 "ftp://ftp.lugroma2.org/pub/gnu/"
170 "ftp://ftp.gnu.inetcosmos.org/pub/gnu/"
171 "ftp://ftp.digitaltrust.it/pub/gnu"
172 "ftp://ftp://rm.mirror.garr.it/mirrors/gnuftp"
173 "ftp://ftp.nluug.nl/pub/gnu/"
174 "ftp://ftp.mirror.nl/pub/mirror/gnu/"
175 "ftp://ftp.nl.uu.net/pub/gnu/"
176 "ftp://mirror.widexs.nl/pub/gnu/"
177 "ftp://ftp.easynet.nl/mirror/GNU/"
178 "ftp://ftp.win.tue.nl/pub/gnu"
179 "ftp://gnu.mirror.vuurwerk.net/pub/GNU/"
180 "ftp://gnu.kookel.org/pub/ftp.gnu.org/"
181 "ftp://ftp.uninett.no/pub/gnu/"
182 "ftp://ftp.task.gda.pl/pub/gnu/"
183 "ftp://sunsite.icm.edu.pl/pub/gnu/"
184 "ftp://ftp.man.poznan.pl/pub/gnu"
185 "ftp://ftp.ist.utl.pt/pub/GNU/gnu/"
186 "ftp://ftp.telepac.pt/pub/gnu/"
187 "ftp://ftp.timisoara.roedu.net/mirrors/ftp.gnu.org/pub/gnu"
188 "ftp://ftp.chg.ru/pub/gnu/"
189 "ftp://gnuftp.axitel.ru/"
190 "ftp://ftp.arnes.si/software/gnu/"
191 "ftp://ftp.etsimo.uniovi.es/pub/gnu/"
192 "ftp://ftp.rediris.es/pub/gnu/"
193 "ftp://ftp.chl.chalmers.se/pub/gnu/"
194 "ftp://ftp.isy.liu.se/pub/gnu/"
195 "ftp://ftp.luth.se/pub/unix/gnu/"
196 "ftp://ftp.stacken.kth.se/pub/gnu/"
197 "ftp://ftp.sunet.se/pub/gnu/"
198 "ftp://sunsite.cnlab-switch.ch/mirror/gnu/"
199 "ftp://ftp.ulak.net.tr/gnu/"
200 "ftp://ftp.gnu.org.ua"
201 "ftp://ftp.mcc.ac.uk/pub/gnu/"
202 "ftp://ftp.mirror.ac.uk/sites/ftp.gnu.org/gnu/"
203 "ftp://ftp.warwick.ac.uk/pub/gnu/"
204 "ftp://ftp.hands.com/ftp.gnu.org/"
205 "ftp://gnu.teleglobe.net/ftp.gnu.org/"])
206 ("GNU Project Home Page" . "www.gnu.org")
207
208 ;; Emacs.
209 ("Emacs Home Page" .
210 "www.gnu.org/software/emacs/emacs.html")
211 ("Savannah Emacs page" .
212 "savannah.gnu.org/projects/emacs")
213 ("Emacs Lisp List" .
214 "www.damtp.cam.ac.uk/user/eglen/emacs/ell.html")
215 ("Emacs Wiki" .
216 [simple-query "www.emacswiki.org"
217 "www.emacswiki.org/cgi-bin/wiki/" ""])
218
219 ;; Internet search engines.
220 ("Google" .
221 [simple-query "www.google.com"
222 "www.google.com/search?q=" ""])
223 ("Google Groups" .
224 [simple-query "groups.google.com"
225 "groups.google.com/groups?q=" ""])
226 ("Yahoo" .
227 [simple-query "www.yahoo.com" "search.yahoo.com/search?p=" ""])
228 ("Yahoo: Reference" . "www.yahoo.com/Reference/")
229 ("Wikipedia" .
230 [simple-query "wikipedia.org" "wikipedia.org/wiki/" ""])
231
232 ;; Misc. general interest.
233 ("Interactive Weather Information Network" . webjump-to-iwin)
234 ("Usenet FAQs" .
235 "www.faqs.org/faqs/")
236 ("RTFM Usenet FAQs by Group" .
237 "ftp://rtfm.mit.edu/pub/usenet-by-group/")
238 ("RTFM Usenet FAQs by Hierarchy" .
239 "ftp://rtfm.mit.edu/pub/usenet-by-hierarchy/")
240 ("X Consortium Archive" . "ftp.x.org")
241
242 ;; Computer social issues, privacy, professionalism.
243 ("Association for Computing Machinery" . "www.acm.org")
244 ("Computer Professionals for Social Responsibility" . "www.cpsr.org")
245 ("Electronic Frontier Foundation" . "www.eff.org")
246 ("IEEE Computer Society" . "www.computer.org")
247 ("Risks Digest" . webjump-to-risks)
248
249 ;; More.
250 ("Supplemental Web site list for webjump" .
251 "www.neilvandyke.org/webjump/")
252
253 )
254 "Sample hotlist for WebJump. See the documentation for the `webjump'
255 function and the `webjump-sites' variable.")
256
257 (defvar webjump-state-to-postal-alist
258 '(("Alabama" . "al") ("Alaska" . "ak") ("Arizona" . "az") ("Arkansas" . "ar")
259 ("California" . "ca") ("Colorado" . "co") ("Connecticut" . "ct")
260 ("Delaware" . "de") ("Florida" . "fl") ("Georgia" . "ga") ("Hawaii" . "hi")
261 ("Idaho" . "id") ("Illinois" . "il") ("Indiana" . "in") ("Iowa" . "ia")
262 ("Kansas" . "ks") ("Kentucky" . "ky") ("Louisiana" . "la") ("Maine" . "me")
263 ("Maryland" . "md") ("Massachusetts" . "ma") ("Michigan" . "mi")
264 ("Minnesota" . "mn") ("Mississippi" . "ms") ("Missouri" . "mo")
265 ("Montana" . "mt") ("Nebraska" . "ne") ("Nevada" . "nv")
266 ("New Hampshire" . "nh") ("New Jersey" . "nj") ("New Mexico" . "nm")
267 ("New York" . "ny") ("North Carolina" . "nc") ("North Dakota" . "nd")
268 ("Ohio" . "oh") ("Oklahoma" . "ok") ("Oregon" . "or")
269 ("Pennsylvania" . "pa") ("Rhode Island" . "ri") ("South Carolina" . "sc")
270 ("South Dakota" . "sd") ("Tennessee" . "tn") ("Texas" . "tx")
271 ("Utah" . "ut") ("Vermont" . "vt") ("Virginia" . "va")
272 ("Washington" . "wa") ("West Virginia" . "wv") ("Wisconsin" . "wi")
273 ("Wyoming" . "wy")))
274
275 ;;------------------------------------------------------------ Option Variables
276
277 (defvar webjump-sites
278 webjump-sample-sites
279 "*Hotlist for WebJump.
280
281 The hotlist is represented as an association list, with the CAR of each cell
282 being the name of the Web site, and the CDR being the definition for the URL of
283 that site. The URL definition can be a string (the URL), a vector (specifying
284 a special \"builtin\" which returns a URL), a symbol (name of a function which
285 returns a URL), or a list (which when `eval'ed yields a URL).
286
287 If the URL definition is a vector, then a \"builtin\" is used. A builtin has a
288 Lisp-like syntax, with the name as the first element of the vector, and any
289 arguments as the following elements. The three current builtins are `name',
290 which returns the name of the site as the URL, `simple-query', which
291 returns a URL that is a function of a query entered by the user, and `mirrors',
292 which allows the user to select from among multiple mirror sites for the same
293 content.
294
295 The first argument to the `simple-query' builtin is a static URL to use if the
296 user enters a blank query. The second and third arguments are the prefix and
297 suffix, respectively, to add to the encoded query the user enters. This
298 builtin covers Web sites that have single-string searches with the query
299 embedded in the URL.
300
301 The arguments to the `mirrors' builtin are URLs of mirror sites.
302
303 If the symbol of a function is given, then the function will be called with the
304 Web site name (the one you specified in the CAR of the alist cell) as a
305 parameter. This might come in handy for various kludges.
306
307 For convenience, if the `http://', `ftp://', or `file://' prefix is missing
308 from a URL, WebJump will make a guess at what you wanted and prepend it before
309 submitting the URL.")
310
311 ;;------------------------------------------------------- Sample Site Functions
312
313 (defun webjump-to-iwin (name)
314 (let ((prefix "http://iwin.nws.noaa.gov/")
315 (state (webjump-read-choice name "state"
316 (append '(("Puerto Rico" . "pr"))
317 webjump-state-to-postal-alist))))
318 (if state
319 (concat prefix "iwin/" state "/"
320 (webjump-read-choice name "option"
321 '(("Hourly Report" . "hourly")
322 ("State Forecast" . "state")
323 ("Local Forecast" . "local")
324 ("Zone Forecast" . "zone")
325 ("Short-Term Forecast" . "shortterm")
326 ("Weather Summary" . "summary")
327 ("Public Information" . "public")
328 ("Climatic Data" . "climate")
329 ("Aviation Products" . "aviation")
330 ("Hydro Products" . "hydro")
331 ("Special Weather" . "special")
332 ("Watches and Warnings" . "warnings"))
333 "zone")
334 ".html")
335 prefix)))
336
337 (defun webjump-to-risks (name)
338 (let (issue volume)
339 (if (and (setq volume (webjump-read-number (concat name " volume")))
340 (setq issue (webjump-read-number (concat name " issue"))))
341 (format "catless.ncl.ac.uk/Risks/%d.%02d.html" volume issue)
342 "catless.ncl.ac.uk/Risks/")))
343
344 ;;-------------------------------------------------------------- Core Functions
345
346 ;;;###autoload
347 (defun webjump ()
348 "Jumps to a Web site from a programmable hotlist.
349
350 See the documentation for the `webjump-sites' variable for how to customize the
351 hotlist.
352
353 Please submit bug reports and other feedback to the author, Neil W. Van Dyke
354 <nwv@acm.org>."
355 (interactive)
356 (let* ((completion-ignore-case t)
357 (item (assoc-string
358 (completing-read "WebJump to site: " webjump-sites nil t)
359 webjump-sites t))
360 (name (car item))
361 (expr (cdr item)))
362 (browse-url (webjump-url-fix
363 (cond ((not expr) "")
364 ((stringp expr) expr)
365 ((vectorp expr) (webjump-builtin expr name))
366 ((listp expr) (eval expr))
367 ((symbolp expr)
368 (if (fboundp expr)
369 (funcall expr name)
370 (error "WebJump URL function \"%s\" undefined"
371 expr)))
372 (t (error "WebJump URL expression for \"%s\" invalid"
373 name)))))))
374
375 (defun webjump-builtin (expr name)
376 (if (< (length expr) 1)
377 (error "WebJump URL builtin for \"%s\" empty" name))
378 (let ((builtin (aref expr 0)))
379 (cond
380 ((eq builtin 'mirrors)
381 (if (= (length expr) 1)
382 (error
383 "WebJump URL builtin \"mirrors\" for \"%s\" needs at least 1 arg"
384 name))
385 (webjump-choose-mirror name (cdr (append expr nil))))
386 ((eq builtin 'name)
387 name)
388 ((eq builtin 'simple-query)
389 (webjump-builtin-check-args expr name 3)
390 (webjump-do-simple-query name (aref expr 1) (aref expr 2) (aref expr 3)))
391 (t (error "WebJump URL builtin \"%s\" for \"%s\" invalid"
392 builtin name)))))
393
394 (defun webjump-builtin-check-args (expr name count)
395 (or (= (length expr) (1+ count))
396 (error "WebJump URL builtin \"%s\" for \"%s\" needs %d args"
397 (aref expr 0) name count)))
398
399 (defun webjump-choose-mirror (name urls)
400 (webjump-read-url-choice (concat name " mirror")
401 urls
402 (webjump-mirror-default urls)))
403
404 (defun webjump-do-simple-query (name noquery-url query-prefix query-suffix)
405 (let ((query (webjump-read-string (concat name " query"))))
406 (if query
407 (concat query-prefix (webjump-url-encode query) query-suffix)
408 noquery-url)))
409
410 (defun webjump-mirror-default (urls)
411 ;; Note: This should be modified to apply some simple kludges/heuristics to
412 ;; pick a site which is likely "close". As a tie-breaker among candidates
413 ;; judged equally desirable, randomness might be used.
414 (car urls))
415
416 (defun webjump-read-choice (name what choices &optional default)
417 (let* ((completion-ignore-case t)
418 (choice (completing-read (concat name " " what ": ") choices nil t)))
419 (if (webjump-null-or-blank-string-p choice)
420 default
421 (cdr (assoc choice choices)))))
422
423 (defun webjump-read-number (prompt)
424 ;; Note: I should make this more robust someday.
425 (let ((input (webjump-read-string prompt)))
426 (if input (string-to-number input))))
427
428 (defun webjump-read-string (prompt)
429 (let ((input (read-string (concat prompt ": "))))
430 (if (webjump-null-or-blank-string-p input) nil input)))
431
432 (defun webjump-read-url-choice (what urls &optional default)
433 ;; Note: Convert this to use `webjump-read-choice' someday.
434 (let* ((completions (mapcar (function (lambda (n) (cons n n)))
435 urls))
436 (input (completing-read (concat what
437 ;;(if default " (RET for default)" "")
438 ": ")
439 completions
440 nil
441 t)))
442 (if (webjump-null-or-blank-string-p input)
443 default
444 (car (assoc input completions)))))
445
446 (defun webjump-null-or-blank-string-p (str)
447 (or (null str) (string-match "^[ \t]*$" str)))
448
449 (defun webjump-url-encode (str)
450 (mapconcat (lambda (c)
451 (let ((s (char-to-string c)))
452 (cond ((string= s " ") "+")
453 ((string-match "[a-zA-Z_.-/]" s) s)
454 (t (upcase (format "%%%02x" c))))))
455 (encode-coding-string str 'utf-8)
456 ""))
457
458 (defun webjump-url-fix (url)
459 (if (webjump-null-or-blank-string-p url)
460 ""
461 (webjump-url-fix-trailing-slash
462 (cond
463 ((string-match "^[a-zA-Z]+:" url) url)
464 ((string-match "^/" url) (concat "file://" url))
465 ((string-match "^\\([^\\./]+\\)" url)
466 (concat (if (string= (downcase (match-string 1 url)) "ftp")
467 "ftp"
468 "http")
469 "://"
470 url))
471 (t url)))))
472
473 (defun webjump-url-fix-trailing-slash (url)
474 (if (string-match "^[a-zA-Z]+://[^/]+$" url)
475 (concat url "/")
476 url))
477
478 ;;-----------------------------------------------------------------------------
479
480 (provide 'webjump)
481
482 ;;; webjump.el ends here