]> code.delx.au - gnu-emacs/blob - lisp/net/webjump.el
* lisp/net/tramp-gvfs.el (tramp-gvfs-mount-spec): Fix typo.
[gnu-emacs] / lisp / net / webjump.el
1 ;;; webjump.el --- programmable Web hotlist
2
3 ;; Copyright (C) 1996-1997, 2001-2016 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 (defgroup webjump nil
71 "Programmable Web hotlist."
72 :prefix "webjump-"
73 :group 'browse-url)
74
75 (defconst webjump-sample-sites
76 '(
77 ;; FSF, not including Emacs-specific.
78 ("GNU Project FTP Archive" .
79 ;; GNU FTP Mirror List from http://www.gnu.org/order/ftp.html
80 [mirrors "ftp://ftp.gnu.org/pub/gnu/"
81 "http://ftpmirror.gnu.org"])
82 ("GNU Project Home Page" . "www.gnu.org")
83
84 ;; Emacs.
85 ("Emacs Home Page" .
86 "www.gnu.org/software/emacs/emacs.html")
87 ("Savannah Emacs page" .
88 "savannah.gnu.org/projects/emacs")
89 ("Emacs Lisp List" .
90 "www.damtp.cam.ac.uk/user/eglen/emacs/ell.html")
91 ("Emacs Wiki" .
92 [simple-query "www.emacswiki.org"
93 "www.emacswiki.org/cgi-bin/wiki/" ""])
94
95 ;; Internet search engines.
96 ("DuckDuckGo" .
97 [simple-query "duckduckgo.com"
98 "duckduckgo.com/?q=" ""])
99 ("Google" .
100 [simple-query "www.google.com"
101 "www.google.com/search?q=" ""])
102 ("Google Groups" .
103 [simple-query "groups.google.com"
104 "groups.google.com/groups?q=" ""])
105 ("Yahoo" .
106 [simple-query "www.yahoo.com" "search.yahoo.com/search?p=" ""])
107 ("Yahoo: Reference" . "www.yahoo.com/Reference/")
108 ("Wikipedia" .
109 [simple-query "wikipedia.org" "wikipedia.org/wiki/" ""])
110
111 ;; Misc. general interest.
112 ("National Weather Service" . webjump-to-iwin)
113 ("Usenet FAQs" .
114 "www.faqs.org/faqs/")
115 ("RTFM Usenet FAQs by Group" .
116 "ftp://rtfm.mit.edu/pub/usenet-by-group/")
117 ("RTFM Usenet FAQs by Hierarchy" .
118 "ftp://rtfm.mit.edu/pub/usenet-by-hierarchy/")
119 ("X Consortium Archive" . "ftp.x.org")
120
121 ;; Computer social issues, privacy, professionalism.
122 ("Association for Computing Machinery" . "www.acm.org")
123 ("Computer Professionals for Social Responsibility" . "www.cpsr.org")
124 ("Electronic Frontier Foundation" . "www.eff.org")
125 ("IEEE Computer Society" . "www.computer.org")
126 ("Risks Digest" . webjump-to-risks)
127
128 ;; More.
129 ("Supplemental Web site list for webjump" .
130 "www.neilvandyke.org/webjump/")
131
132 )
133 "Sample hotlist for WebJump.
134 See the documentation for `webjump' and `webjump-sites'.")
135
136 (defconst webjump-state-to-postal-alist
137 '(("Alabama" . "al") ("Alaska" . "ak") ("Arizona" . "az") ("Arkansas" . "ar")
138 ("California" . "ca") ("Colorado" . "co") ("Connecticut" . "ct")
139 ("Delaware" . "de") ("Florida" . "fl") ("Georgia" . "ga") ("Hawaii" . "hi")
140 ("Idaho" . "id") ("Illinois" . "il") ("Indiana" . "in") ("Iowa" . "ia")
141 ("Kansas" . "ks") ("Kentucky" . "ky") ("Louisiana" . "la") ("Maine" . "me")
142 ("Maryland" . "md") ("Massachusetts" . "ma") ("Michigan" . "mi")
143 ("Minnesota" . "mn") ("Mississippi" . "ms") ("Missouri" . "mo")
144 ("Montana" . "mt") ("Nebraska" . "ne") ("Nevada" . "nv")
145 ("New Hampshire" . "nh") ("New Jersey" . "nj") ("New Mexico" . "nm")
146 ("New York" . "ny") ("North Carolina" . "nc") ("North Dakota" . "nd")
147 ("Ohio" . "oh") ("Oklahoma" . "ok") ("Oregon" . "or")
148 ("Pennsylvania" . "pa") ("Rhode Island" . "ri") ("South Carolina" . "sc")
149 ("South Dakota" . "sd") ("Tennessee" . "tn") ("Texas" . "tx")
150 ("Utah" . "ut") ("Vermont" . "vt") ("Virginia" . "va")
151 ("Washington" . "wa") ("West Virginia" . "wv") ("Wisconsin" . "wi")
152 ("Wyoming" . "wy")))
153
154 ;;------------------------------------------------------------ Option Variables
155
156 (defcustom webjump-sites webjump-sample-sites
157 "Hotlist for WebJump.
158
159 The hotlist is represented as an association list, with the CAR of each cell
160 being the name of the Web site, and the CDR being the definition for the URL of
161 that site. The URL definition can be a string (the URL), a vector (specifying
162 a special \"builtin\" which returns a URL), a symbol (name of a function which
163 returns a URL), or a list (which when `eval'ed yields a URL).
164
165 If the URL definition is a vector, then a \"builtin\" is used. A builtin has a
166 Lisp-like syntax, with the name as the first element of the vector, and any
167 arguments as the following elements. The three current builtins are `name',
168 which returns the name of the site as the URL, `simple-query', which
169 returns a URL that is a function of a query entered by the user, and `mirrors',
170 which allows the user to select from among multiple mirror sites for the same
171 content.
172
173 The first argument to the `simple-query' builtin is a static URL to use if the
174 user enters a blank query. The second and third arguments are the prefix and
175 suffix, respectively, to add to the encoded query the user enters. This
176 builtin covers Web sites that have single-string searches with the query
177 embedded in the URL.
178
179 The arguments to the `mirrors' builtin are URLs of mirror sites.
180
181 If the symbol of a function is given, then the function will be called with the
182 Web site name (the one you specified in the CAR of the alist cell) as a
183 parameter. This might come in handy for various kludges.
184
185 For convenience, if the `http://', `ftp://', or `file://' prefix is missing
186 from a URL, WebJump will make a guess at what you wanted and prepend it before
187 submitting the URL."
188 :type '(alist :key-type (string :tag "Name")
189 :value-type (choice :tag "URL"
190 (string :tag "URL")
191 function
192 (vector :tag "Builtin"
193 (symbol :tag "Name")
194 (repeat :inline t :tag "Arguments"
195 string))
196 (sexp :tag "Expression to eval"))))
197
198 ;;------------------------------------------------------- Sample Site Functions
199
200 (defun webjump-to-iwin (name)
201 (let* ((prefix "http://www.nws.noaa.gov/view/")
202 (state (webjump-read-choice name "state"
203 (append '(("Puerto Rico" . "pr")
204 ("Guam" . "gu")
205 ("American Samoa" . "as")
206 ("District of Columbia" . "dc")
207 ("US Virgin Islands" . "vi"))
208 webjump-state-to-postal-alist)))
209 (opt (if state
210 (webjump-read-choice
211 name "option"
212 '(("Hourly Report" . "hourly")
213 ("State Forecast" . "state")
214 ("Zone Forecast" . "zone")
215 ("Short-Term Forecast" . "shortterm")
216 ("Forecast Discussion" . "discussion")
217 ("Weather Summary" . "summary")
218 ("Public Information" . "public")
219 ("Climatic Data" . "climate")
220 ("Hydro Products" . "hydro")
221 ("Watches" . "watches")
222 ("Special Weather" . "special")
223 ("Warnings and Advisories" . "warnings")
224 ("Fire Weather" . "firewx"))))))
225 (cond (opt (concat prefix "prodsByState.php?state=" state "&prodtype=" opt))
226 (state (concat prefix "states.php?state=" state))
227 (t prefix))))
228
229 (defun webjump-to-risks (name)
230 (let (issue volume)
231 (if (and (setq volume (webjump-read-number (concat name " volume")))
232 (setq issue (webjump-read-number (concat name " issue"))))
233 (format "catless.ncl.ac.uk/Risks/%d.%02d.html" volume issue)
234 "catless.ncl.ac.uk/Risks/")))
235
236 ;;-------------------------------------------------------------- Core Functions
237
238 ;;;###autoload
239 (defun webjump ()
240 "Jumps to a Web site from a programmable hotlist.
241
242 See the documentation for the `webjump-sites' variable for how to customize the
243 hotlist.
244
245 Please submit bug reports and other feedback to the author, Neil W. Van Dyke
246 <nwv@acm.org>."
247 (interactive)
248 (let* ((completion-ignore-case t)
249 (item (assoc-string
250 (completing-read "WebJump to site: " webjump-sites nil t)
251 webjump-sites t))
252 (name (car item))
253 (expr (cdr item)))
254 (browse-url (webjump-url-fix
255 (cond ((not expr) "")
256 ((stringp expr) expr)
257 ((vectorp expr) (webjump-builtin expr name))
258 ((listp expr) (eval expr))
259 ((symbolp expr)
260 (if (fboundp expr)
261 (funcall expr name)
262 (error "WebJump URL function \"%s\" undefined"
263 expr)))
264 (t (error "WebJump URL expression for \"%s\" invalid"
265 name)))))))
266
267 (defun webjump-builtin (expr name)
268 (if (< (length expr) 1)
269 (error "WebJump URL builtin for \"%s\" empty" name))
270 (let ((builtin (aref expr 0)))
271 (cond
272 ((eq builtin 'mirrors)
273 (if (= (length expr) 1)
274 (error
275 "WebJump URL builtin \"mirrors\" for \"%s\" needs at least 1 arg"
276 name))
277 (webjump-choose-mirror name (cdr (append expr nil))))
278 ((eq builtin 'name)
279 name)
280 ((eq builtin 'simple-query)
281 (webjump-builtin-check-args expr name 3)
282 (webjump-do-simple-query name (aref expr 1) (aref expr 2) (aref expr 3)))
283 (t (error "WebJump URL builtin \"%s\" for \"%s\" invalid"
284 builtin name)))))
285
286 (defun webjump-builtin-check-args (expr name count)
287 (or (= (length expr) (1+ count))
288 (error "WebJump URL builtin \"%s\" for \"%s\" needs %d args"
289 (aref expr 0) name count)))
290
291 (defun webjump-choose-mirror (name urls)
292 (webjump-read-url-choice (concat name " mirror")
293 urls
294 (webjump-mirror-default urls)))
295
296 (defun webjump-do-simple-query (name noquery-url query-prefix query-suffix)
297 (let ((query (webjump-read-string (concat name " query"))))
298 (if query
299 (concat query-prefix (webjump-url-encode query) query-suffix)
300 noquery-url)))
301
302 (defun webjump-mirror-default (urls)
303 ;; Note: This should be modified to apply some simple kludges/heuristics to
304 ;; pick a site which is likely "close". As a tie-breaker among candidates
305 ;; judged equally desirable, randomness might be used.
306 (car urls))
307
308 (defun webjump-read-choice (name what choices &optional default)
309 (let* ((completion-ignore-case t)
310 (choice (completing-read (concat name " " what ": ") choices nil t)))
311 (if (webjump-null-or-blank-string-p choice)
312 default
313 (cdr (assoc choice choices)))))
314
315 (defun webjump-read-number (prompt)
316 ;; Note: I should make this more robust someday.
317 (let ((input (webjump-read-string prompt)))
318 (if input (string-to-number input))))
319
320 (defun webjump-read-string (prompt)
321 (let ((input (read-string (concat prompt ": "))))
322 (if (webjump-null-or-blank-string-p input) nil input)))
323
324 (defun webjump-read-url-choice (what urls &optional default)
325 ;; Note: Convert this to use `webjump-read-choice' someday.
326 (let* ((completions (mapcar (function (lambda (n) (cons n n)))
327 urls))
328 (input (completing-read (concat what
329 ;;(if default " (RET for default)" "")
330 ": ")
331 completions
332 nil
333 t)))
334 (if (webjump-null-or-blank-string-p input)
335 default
336 (car (assoc input completions)))))
337
338 (defun webjump-null-or-blank-string-p (str)
339 (or (null str) (string-match "^[ \t]*$" str)))
340
341 (defun webjump-url-encode (str)
342 (mapconcat (lambda (c)
343 (let ((s (char-to-string c)))
344 (cond ((string= s " ") "+")
345 ((string-match "[a-zA-Z_.-/]" s) s)
346 (t (upcase (format "%%%02x" c))))))
347 (encode-coding-string str 'utf-8)
348 ""))
349
350 (defun webjump-url-fix (url)
351 (if (webjump-null-or-blank-string-p url)
352 ""
353 (webjump-url-fix-trailing-slash
354 (cond
355 ((string-match "^[a-zA-Z]+:" url) url)
356 ((string-match "^/" url) (concat "file://" url))
357 ((string-match "^\\([^\\./]+\\)" url)
358 (concat (if (string= (downcase (match-string 1 url)) "ftp")
359 "ftp"
360 "http")
361 "://"
362 url))
363 (t url)))))
364
365 (defun webjump-url-fix-trailing-slash (url)
366 (if (string-match "^[a-zA-Z]+://[^/]+$" url)
367 (concat url "/")
368 url))
369
370 ;;-----------------------------------------------------------------------------
371
372 (provide 'webjump)
373
374 ;;; webjump.el ends here