]> code.delx.au - gnu-emacs/blob - lisp/net/webjump.el
f3626a711ef1c422a90efe4872207c6d886791c6
[gnu-emacs] / lisp / net / webjump.el
1 ;;; webjump.el --- programmable Web hotlist
2
3 ;; Copyright (C) 1996-1997, 2001-2014 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 init 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 ("DuckDuckGo" .
221 [simple-query "duckduckgo.com"
222 "duckduckgo.com/?q=" ""])
223 ("Google" .
224 [simple-query "www.google.com"
225 "www.google.com/search?q=" ""])
226 ("Google Groups" .
227 [simple-query "groups.google.com"
228 "groups.google.com/groups?q=" ""])
229 ("Yahoo" .
230 [simple-query "www.yahoo.com" "search.yahoo.com/search?p=" ""])
231 ("Yahoo: Reference" . "www.yahoo.com/Reference/")
232 ("Wikipedia" .
233 [simple-query "wikipedia.org" "wikipedia.org/wiki/" ""])
234
235 ;; Misc. general interest.
236 ("Interactive Weather Information Network" . webjump-to-iwin)
237 ("Usenet FAQs" .
238 "www.faqs.org/faqs/")
239 ("RTFM Usenet FAQs by Group" .
240 "ftp://rtfm.mit.edu/pub/usenet-by-group/")
241 ("RTFM Usenet FAQs by Hierarchy" .
242 "ftp://rtfm.mit.edu/pub/usenet-by-hierarchy/")
243 ("X Consortium Archive" . "ftp.x.org")
244
245 ;; Computer social issues, privacy, professionalism.
246 ("Association for Computing Machinery" . "www.acm.org")
247 ("Computer Professionals for Social Responsibility" . "www.cpsr.org")
248 ("Electronic Frontier Foundation" . "www.eff.org")
249 ("IEEE Computer Society" . "www.computer.org")
250 ("Risks Digest" . webjump-to-risks)
251
252 ;; More.
253 ("Supplemental Web site list for webjump" .
254 "www.neilvandyke.org/webjump/")
255
256 )
257 "Sample hotlist for WebJump. See the documentation for the `webjump'
258 function and the `webjump-sites' variable.")
259
260 (defvar webjump-state-to-postal-alist
261 '(("Alabama" . "al") ("Alaska" . "ak") ("Arizona" . "az") ("Arkansas" . "ar")
262 ("California" . "ca") ("Colorado" . "co") ("Connecticut" . "ct")
263 ("Delaware" . "de") ("Florida" . "fl") ("Georgia" . "ga") ("Hawaii" . "hi")
264 ("Idaho" . "id") ("Illinois" . "il") ("Indiana" . "in") ("Iowa" . "ia")
265 ("Kansas" . "ks") ("Kentucky" . "ky") ("Louisiana" . "la") ("Maine" . "me")
266 ("Maryland" . "md") ("Massachusetts" . "ma") ("Michigan" . "mi")
267 ("Minnesota" . "mn") ("Mississippi" . "ms") ("Missouri" . "mo")
268 ("Montana" . "mt") ("Nebraska" . "ne") ("Nevada" . "nv")
269 ("New Hampshire" . "nh") ("New Jersey" . "nj") ("New Mexico" . "nm")
270 ("New York" . "ny") ("North Carolina" . "nc") ("North Dakota" . "nd")
271 ("Ohio" . "oh") ("Oklahoma" . "ok") ("Oregon" . "or")
272 ("Pennsylvania" . "pa") ("Rhode Island" . "ri") ("South Carolina" . "sc")
273 ("South Dakota" . "sd") ("Tennessee" . "tn") ("Texas" . "tx")
274 ("Utah" . "ut") ("Vermont" . "vt") ("Virginia" . "va")
275 ("Washington" . "wa") ("West Virginia" . "wv") ("Wisconsin" . "wi")
276 ("Wyoming" . "wy")))
277
278 ;;------------------------------------------------------------ Option Variables
279
280 (defvar webjump-sites
281 webjump-sample-sites
282 "Hotlist for WebJump.
283
284 The hotlist is represented as an association list, with the CAR of each cell
285 being the name of the Web site, and the CDR being the definition for the URL of
286 that site. The URL definition can be a string (the URL), a vector (specifying
287 a special \"builtin\" which returns a URL), a symbol (name of a function which
288 returns a URL), or a list (which when `eval'ed yields a URL).
289
290 If the URL definition is a vector, then a \"builtin\" is used. A builtin has a
291 Lisp-like syntax, with the name as the first element of the vector, and any
292 arguments as the following elements. The three current builtins are `name',
293 which returns the name of the site as the URL, `simple-query', which
294 returns a URL that is a function of a query entered by the user, and `mirrors',
295 which allows the user to select from among multiple mirror sites for the same
296 content.
297
298 The first argument to the `simple-query' builtin is a static URL to use if the
299 user enters a blank query. The second and third arguments are the prefix and
300 suffix, respectively, to add to the encoded query the user enters. This
301 builtin covers Web sites that have single-string searches with the query
302 embedded in the URL.
303
304 The arguments to the `mirrors' builtin are URLs of mirror sites.
305
306 If the symbol of a function is given, then the function will be called with the
307 Web site name (the one you specified in the CAR of the alist cell) as a
308 parameter. This might come in handy for various kludges.
309
310 For convenience, if the `http://', `ftp://', or `file://' prefix is missing
311 from a URL, WebJump will make a guess at what you wanted and prepend it before
312 submitting the URL.")
313
314 ;;------------------------------------------------------- Sample Site Functions
315
316 (defun webjump-to-iwin (name)
317 (let ((prefix "http://iwin.nws.noaa.gov/")
318 (state (webjump-read-choice name "state"
319 (append '(("Puerto Rico" . "pr"))
320 webjump-state-to-postal-alist))))
321 (if state
322 (concat prefix "iwin/" state "/"
323 (webjump-read-choice name "option"
324 '(("Hourly Report" . "hourly")
325 ("State Forecast" . "state")
326 ("Local Forecast" . "local")
327 ("Zone Forecast" . "zone")
328 ("Short-Term Forecast" . "shortterm")
329 ("Weather Summary" . "summary")
330 ("Public Information" . "public")
331 ("Climatic Data" . "climate")
332 ("Aviation Products" . "aviation")
333 ("Hydro Products" . "hydro")
334 ("Special Weather" . "special")
335 ("Watches and Warnings" . "warnings"))
336 "zone")
337 ".html")
338 prefix)))
339
340 (defun webjump-to-risks (name)
341 (let (issue volume)
342 (if (and (setq volume (webjump-read-number (concat name " volume")))
343 (setq issue (webjump-read-number (concat name " issue"))))
344 (format "catless.ncl.ac.uk/Risks/%d.%02d.html" volume issue)
345 "catless.ncl.ac.uk/Risks/")))
346
347 ;;-------------------------------------------------------------- Core Functions
348
349 ;;;###autoload
350 (defun webjump ()
351 "Jumps to a Web site from a programmable hotlist.
352
353 See the documentation for the `webjump-sites' variable for how to customize the
354 hotlist.
355
356 Please submit bug reports and other feedback to the author, Neil W. Van Dyke
357 <nwv@acm.org>."
358 (interactive)
359 (let* ((completion-ignore-case t)
360 (item (assoc-string
361 (completing-read "WebJump to site: " webjump-sites nil t)
362 webjump-sites t))
363 (name (car item))
364 (expr (cdr item)))
365 (browse-url (webjump-url-fix
366 (cond ((not expr) "")
367 ((stringp expr) expr)
368 ((vectorp expr) (webjump-builtin expr name))
369 ((listp expr) (eval expr))
370 ((symbolp expr)
371 (if (fboundp expr)
372 (funcall expr name)
373 (error "WebJump URL function \"%s\" undefined"
374 expr)))
375 (t (error "WebJump URL expression for \"%s\" invalid"
376 name)))))))
377
378 (defun webjump-builtin (expr name)
379 (if (< (length expr) 1)
380 (error "WebJump URL builtin for \"%s\" empty" name))
381 (let ((builtin (aref expr 0)))
382 (cond
383 ((eq builtin 'mirrors)
384 (if (= (length expr) 1)
385 (error
386 "WebJump URL builtin \"mirrors\" for \"%s\" needs at least 1 arg"
387 name))
388 (webjump-choose-mirror name (cdr (append expr nil))))
389 ((eq builtin 'name)
390 name)
391 ((eq builtin 'simple-query)
392 (webjump-builtin-check-args expr name 3)
393 (webjump-do-simple-query name (aref expr 1) (aref expr 2) (aref expr 3)))
394 (t (error "WebJump URL builtin \"%s\" for \"%s\" invalid"
395 builtin name)))))
396
397 (defun webjump-builtin-check-args (expr name count)
398 (or (= (length expr) (1+ count))
399 (error "WebJump URL builtin \"%s\" for \"%s\" needs %d args"
400 (aref expr 0) name count)))
401
402 (defun webjump-choose-mirror (name urls)
403 (webjump-read-url-choice (concat name " mirror")
404 urls
405 (webjump-mirror-default urls)))
406
407 (defun webjump-do-simple-query (name noquery-url query-prefix query-suffix)
408 (let ((query (webjump-read-string (concat name " query"))))
409 (if query
410 (concat query-prefix (webjump-url-encode query) query-suffix)
411 noquery-url)))
412
413 (defun webjump-mirror-default (urls)
414 ;; Note: This should be modified to apply some simple kludges/heuristics to
415 ;; pick a site which is likely "close". As a tie-breaker among candidates
416 ;; judged equally desirable, randomness might be used.
417 (car urls))
418
419 (defun webjump-read-choice (name what choices &optional default)
420 (let* ((completion-ignore-case t)
421 (choice (completing-read (concat name " " what ": ") choices nil t)))
422 (if (webjump-null-or-blank-string-p choice)
423 default
424 (cdr (assoc choice choices)))))
425
426 (defun webjump-read-number (prompt)
427 ;; Note: I should make this more robust someday.
428 (let ((input (webjump-read-string prompt)))
429 (if input (string-to-number input))))
430
431 (defun webjump-read-string (prompt)
432 (let ((input (read-string (concat prompt ": "))))
433 (if (webjump-null-or-blank-string-p input) nil input)))
434
435 (defun webjump-read-url-choice (what urls &optional default)
436 ;; Note: Convert this to use `webjump-read-choice' someday.
437 (let* ((completions (mapcar (function (lambda (n) (cons n n)))
438 urls))
439 (input (completing-read (concat what
440 ;;(if default " (RET for default)" "")
441 ": ")
442 completions
443 nil
444 t)))
445 (if (webjump-null-or-blank-string-p input)
446 default
447 (car (assoc input completions)))))
448
449 (defun webjump-null-or-blank-string-p (str)
450 (or (null str) (string-match "^[ \t]*$" str)))
451
452 (defun webjump-url-encode (str)
453 (mapconcat (lambda (c)
454 (let ((s (char-to-string c)))
455 (cond ((string= s " ") "+")
456 ((string-match "[a-zA-Z_.-/]" s) s)
457 (t (upcase (format "%%%02x" c))))))
458 (encode-coding-string str 'utf-8)
459 ""))
460
461 (defun webjump-url-fix (url)
462 (if (webjump-null-or-blank-string-p url)
463 ""
464 (webjump-url-fix-trailing-slash
465 (cond
466 ((string-match "^[a-zA-Z]+:" url) url)
467 ((string-match "^/" url) (concat "file://" url))
468 ((string-match "^\\([^\\./]+\\)" url)
469 (concat (if (string= (downcase (match-string 1 url)) "ftp")
470 "ftp"
471 "http")
472 "://"
473 url))
474 (t url)))))
475
476 (defun webjump-url-fix-trailing-slash (url)
477 (if (string-match "^[a-zA-Z]+://[^/]+$" url)
478 (concat url "/")
479 url))
480
481 ;;-----------------------------------------------------------------------------
482
483 (provide 'webjump)
484
485 ;;; webjump.el ends here