(find-buffer-file-type-coding-system,
[bpt/emacs.git] / lisp / webjump.el
CommitLineData
3b369e52
RS
1;;; webjump.el --- programmable Web hotlist
2
e9f527a0 3;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3b369e52
RS
4
5;; Author: Neil W. Van Dyke <nwv@acm.org>
e9f527a0 6;; Created: 09-Aug-1996
3b369e52 7;; Keywords: comm www
3b369e52
RS
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;;
e9f527a0 45;; (require 'webjump)
3b369e52
RS
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;;
e9f527a0
RS
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.
3b369e52
RS
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
3b369e52
RS
64;;; Code:
65
66;;-------------------------------------------------------- Package Dependencies
67
68(require 'browse-url)
69
70;;------------------------------------------------------------------- Constants
71
8a5f6f7f 72(defvar webjump-sample-sites
e9f527a0
RS
73 '(
74
75 ;; FSF, not including Emacs-specific.
76 ("GNU Project FTP Archive" .
77 [mirrors "ftp://prep.ai.mit.edu/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"
112 ;; SOUTH AMERICA:
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.ai.mit.edu")
130
131 ;; Emacs.
132 ("Emacs Lisp Archive" .
133 "ftp://archive.cis.ohio-state.edu/pub/gnu/emacs/elisp-archive/")
134
135 ;; Internet search engines.
136 ("AltaVista" .
3b369e52
RS
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" .
e9f527a0
RS
142 [simple-query "hoohoo.ncsa.uiuc.edu/cgi-bin/AA.pl"
143 "hoohoo.ncsa.uiuc.edu/cgi-bin/AA.pl?query=" ""])
3b369e52 144 ("Lycos" .
e9f527a0
RS
145 [simple-query "www.lycos.com"
146 "www.lycos.com/cgi-bin/pursuit?cat=lycos&query=" ""])
147 ("Yahoo" .
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)
3b369e52
RS
152 ("Usenet FAQs" .
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 ""])
3b369e52
RS
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/")
e9f527a0
RS
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
3b369e52 173 )
e9f527a0
RS
174 "Sample hotlist for WebJump. See the documentation for the `webjump'
175function and the `webjump-sites' variable.")
3b369e52 176
8a5f6f7f 177(defvar webjump-state-to-postal-alist
3b369e52
RS
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
3b369e52
RS
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
e9f527a0
RS
273Please submit bug reports and other feedback to the author, Neil W. Van Dyke
274<nwv@acm.org>."
3b369e52
RS
275 (interactive)
276 (let* ((completion-ignore-case t)
277 (item (assoc (completing-read "WebJump to site: " webjump-sites nil t)
278 webjump-sites))
279 (name (car item))
280 (expr (cdr item)))
281 (funcall browse-url-browser-function
282 (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)
290 (error "WebJump URL function \"%s\" undefined." expr)))
291 (t (error "WebJump URL expression for \"%s\" invalid."
292 name)))))))
293
3b369e52
RS
294(defun webjump-builtin (expr name)
295 (if (< (length expr) 1)
296 (error "WebJump URL builtin for \"%s\" empty." name))
297 (let ((builtin (aref expr 0)))
298 (cond
299 ((eq builtin 'mirrors)
300 (if (= (length expr) 1)
301 (error
302 "WebJump URL builtin \"mirrors\" for \"%s\" needs at least 1 arg."))
303 (webjump-choose-mirror name (cdr (append expr nil))))
304 ((eq builtin 'name)
305 name)
306 ((eq builtin 'simple-query)
307 (webjump-builtin-check-args expr name 3)
308 (webjump-do-simple-query name (aref expr 1) (aref expr 2) (aref expr 3)))
309 (t (error "WebJump URL builtin \"%s\" for \"%s\" invalid."
310 builtin name)))))
311
312(defun webjump-builtin-check-args (expr name count)
313 (or (= (length expr) (1+ count))
314 (error "WebJump URL builtin \"%s\" for \"%s\" needs %d args."
315 (aref expr 0) name count)))
316
317(defun webjump-choose-mirror (name urls)
318 (webjump-read-url-choice (concat name " mirror")
319 urls
320 (webjump-mirror-default urls)))
321
322(defun webjump-do-simple-query (name noquery-url query-prefix query-suffix)
323 (let ((query (webjump-read-string (concat name " query"))))
324 (if query
325 (concat query-prefix (webjump-url-encode query) query-suffix)
326 noquery-url)))
327
328(defun webjump-mirror-default (urls)
329 ;; Note: This should be modified to apply some simple kludges/heuristics to
330 ;; pick a site which is likely "close". As a tie-breaker among candidates
e9f527a0 331 ;; judged equally desirable, randomness might be used.
3b369e52
RS
332 (car urls))
333
334(defun webjump-read-choice (name what choices &optional default)
335 (let* ((completion-ignore-case t)
336 (choice (completing-read (concat name " " what ": ") choices nil t)))
337 (if (webjump-null-or-blank-string-p choice)
338 default
339 (cdr (assoc choice choices)))))
340
341(defun webjump-read-number (prompt)
342 ;; Note: I should make this more robust someday.
343 (let ((input (webjump-read-string prompt)))
344 (if input (string-to-number input))))
345
346(defun webjump-read-string (prompt)
347 (let ((input (read-string (concat prompt ": "))))
348 (if (webjump-null-or-blank-string-p input) nil input)))
349
350(defun webjump-read-url-choice (what urls &optional default)
351 ;; Note: Convert this to use `webjump-read-choice' someday.
352 (let* ((completions (mapcar (function (lambda (n) (cons n n)))
353 urls))
354 (input (completing-read (concat what
355 ;;(if default " (RET for default)" "")
356 ": ")
357 completions
358 nil
359 t)))
360 (if (webjump-null-or-blank-string-p input)
361 default
362 (car (assoc input completions)))))
363
364(defun webjump-null-or-blank-string-p (str)
365 (or (null str) (string-match "^[ \t]*$" str)))
366
367(defun webjump-url-encode (str)
368 (mapconcat '(lambda (c)
369 (cond ((= c 32) "+")
370 ((or (and (>= c ?a) (<= c ?z))
371 (and (>= c ?A) (<= c ?Z))
372 (and (>= c ?0) (<= c ?9)))
373 (char-to-string c))
374 (t (upcase (format "%%%02x" c)))))
375 str
376 ""))
377
378(defun webjump-url-fix (url)
379 (if (webjump-null-or-blank-string-p url)
380 ""
381 (webjump-url-fix-trailing-slash
382 (cond
383 ((string-match "^[a-zA-Z]+:" url) url)
384 ((string-match "^/" url) (concat "file://" url))
385 ((string-match "^\\([^\\./]+\\)" url)
386 (concat (if (string= (downcase (match-string 1 url)) "ftp")
387 "ftp"
388 "http")
389 "://"
390 url))
391 (t url)))))
392
393(defun webjump-url-fix-trailing-slash (url)
394 (if (string-match "^[a-zA-Z]+://[^/]+$" url)
395 (concat url "/")
396 url))
397
398;;-----------------------------------------------------------------------------
399
400(provide 'webjump)
401
402;; webjump.el ends here