1 ;;; webjump.el --- programmable Web hotlist
3 ;; Copyright (C) 1996-1997, 2001-2017 Free Software Foundation, Inc.
5 ;; Author: Neil W. Van Dyke <nwv@acm.org>
6 ;; Created: 09-Aug-1996
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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.
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.)
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.
41 ;; You may wish to add something like the following to your init file:
44 ;; (global-set-key "\C-cj" 'webjump)
45 ;; (setq webjump-sites
47 ;; ("My Home Page" . "www.someisp.net/users/joebobjr/")
48 ;; ("Pop's Site" . "www.joebob-and-son.com/")
50 ;; webjump-sample-sites))
52 ;; The above loads this package, binds `C-c j' to invoke WebJump, and adds your
53 ;; personal favorite sites to the hotlist.
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.
59 ;; The `browse-url' package is used to submit URLs to the browser, so any
60 ;; browser-specific configuration should be done there.
64 ;;-------------------------------------------------------- Package Dependencies
68 ;;------------------------------------------------------------------- Constants
71 "Programmable Web hotlist."
75 (defconst webjump-sample-sites
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")
86 "www.gnu.org/software/emacs/emacs.html")
87 ("Savannah Emacs page" .
88 "savannah.gnu.org/projects/emacs")
90 "www.damtp.cam.ac.uk/user/eglen/emacs/ell.html")
92 [simple-query
"www.emacswiki.org"
93 "www.emacswiki.org/cgi-bin/wiki/" ""])
95 ;; Internet search engines.
97 [simple-query
"duckduckgo.com"
98 "duckduckgo.com/?q=" ""])
100 [simple-query
"www.google.com"
101 "www.google.com/search?q=" ""])
103 [simple-query
"groups.google.com"
104 "groups.google.com/groups?q=" ""])
106 [simple-query
"www.yahoo.com" "search.yahoo.com/search?p=" ""])
107 ("Yahoo: Reference" .
"www.yahoo.com/Reference/")
109 [simple-query
"wikipedia.org" "wikipedia.org/wiki/" ""])
111 ;; Misc. general interest.
112 ("National Weather Service" . webjump-to-iwin
)
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")
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
)
129 ("Supplemental Web site list for webjump" .
130 "www.neilvandyke.org/webjump/")
133 "Sample hotlist for WebJump.
134 See the documentation for `webjump' and `webjump-sites'.")
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")
154 ;;------------------------------------------------------------ Option Variables
156 (defcustom webjump-sites webjump-sample-sites
157 "Hotlist for WebJump.
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).
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
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
179 The arguments to the `mirrors' builtin are URLs of mirror sites.
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.
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
188 :type
'(alist :key-type
(string :tag
"Name")
189 :value-type
(choice :tag
"URL"
192 (vector :tag
"Builtin"
194 (repeat :inline t
:tag
"Arguments"
196 (sexp :tag
"Expression to eval"))))
198 ;;------------------------------------------------------- Sample Site Functions
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")
205 ("American Samoa" .
"as")
206 ("District of Columbia" .
"dc")
207 ("US Virgin Islands" .
"vi"))
208 webjump-state-to-postal-alist
)))
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
))
229 (defun webjump-to-risks (name)
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/")))
236 ;;-------------------------------------------------------------- Core Functions
240 "Jumps to a Web site from a programmable hotlist.
242 See the documentation for the `webjump-sites' variable for how to customize the
245 Please submit bug reports and other feedback to the author, Neil W. Van Dyke
248 (let* ((completion-ignore-case t
)
250 (completing-read "WebJump to site: " webjump-sites nil t
)
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
))
262 (error "WebJump URL function \"%s\" undefined"
264 (t (error "WebJump URL expression for \"%s\" invalid"
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)))
272 ((eq builtin
'mirrors
)
273 (if (= (length expr
) 1)
275 "WebJump URL builtin \"mirrors\" for \"%s\" needs at least 1 arg"
277 (webjump-choose-mirror name
(cdr (append expr nil
))))
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"
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
)))
291 (defun webjump-choose-mirror (name urls
)
292 (webjump-read-url-choice (concat name
" mirror")
294 (webjump-mirror-default urls
)))
296 (defun webjump-do-simple-query (name noquery-url query-prefix query-suffix
)
297 (let ((query (webjump-read-string (concat name
" query"))))
299 (concat query-prefix
(webjump-url-encode query
) query-suffix
)
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.
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
)
313 (cdr (assoc choice choices
)))))
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
))))
320 (defun webjump-read-string (prompt)
321 (let ((input (read-string (concat prompt
": "))))
322 (if (webjump-null-or-blank-string-p input
) nil input
)))
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
)))
328 (input (completing-read (concat what
329 ;;(if default " (RET for default)" "")
334 (if (webjump-null-or-blank-string-p input
)
336 (car (assoc input completions
)))))
338 (defun webjump-null-or-blank-string-p (str)
339 (or (null str
) (string-match "^[ \t]*$" str
)))
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
)
350 (defun webjump-url-fix (url)
351 (if (webjump-null-or-blank-string-p url
)
353 (webjump-url-fix-trailing-slash
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")
365 (defun webjump-url-fix-trailing-slash (url)
366 (if (string-match "^[a-zA-Z]+://[^/]+$" url
)
370 ;;-----------------------------------------------------------------------------
374 ;;; webjump.el ends here