webjump.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. ;;; webjump.el --- programmable Web hotlist
  2. ;; Copyright (C) 1996-1997, 2001-2017 Free Software Foundation, Inc.
  3. ;; Author: Neil W. Van Dyke <nwv@acm.org>
  4. ;; Created: 09-Aug-1996
  5. ;; Keywords: comm www
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; WebJump provides a sort of ``programmable hotlist'' of Web sites that can
  19. ;; quickly be invoked in your Web browser. Each Web site in the hotlist has a
  20. ;; name, and you select the desired site name via a completing string prompt in
  21. ;; the minibuffer. The URL for each Web site is defined as a static string or
  22. ;; a built-in or custom function, allowing interactive prompting for
  23. ;; site-specific queries and options.
  24. ;; Note that WebJump was originally intended to complement your conventional
  25. ;; browser-based hotlist, not replace it. (Though there's no reason you
  26. ;; couldn't use WebJump for your entire hotlist if you were so inclined.)
  27. ;; The `webjump-sites' variable, which defines the hotlist, defaults to some
  28. ;; example sites. You'll probably want to override it with your own favorite
  29. ;; sites. The documentation for the variable describes the syntax.
  30. ;; You may wish to add something like the following to your init file:
  31. ;;
  32. ;; (require 'webjump)
  33. ;; (global-set-key "\C-cj" 'webjump)
  34. ;; (setq webjump-sites
  35. ;; (append '(
  36. ;; ("My Home Page" . "www.someisp.net/users/joebobjr/")
  37. ;; ("Pop's Site" . "www.joebob-and-son.com/")
  38. ;; )
  39. ;; webjump-sample-sites))
  40. ;;
  41. ;; The above loads this package, binds `C-c j' to invoke WebJump, and adds your
  42. ;; personal favorite sites to the hotlist.
  43. ;; The `webjump-sample-sites' variable mostly contains some site entries that
  44. ;; are expected to be generally relevant to many users, but excluding
  45. ;; those that the GNU project would not want to recommend.
  46. ;; The `browse-url' package is used to submit URLs to the browser, so any
  47. ;; browser-specific configuration should be done there.
  48. ;;; Code:
  49. ;;-------------------------------------------------------- Package Dependencies
  50. (require 'browse-url)
  51. ;;------------------------------------------------------------------- Constants
  52. (defgroup webjump nil
  53. "Programmable Web hotlist."
  54. :prefix "webjump-"
  55. :group 'browse-url)
  56. (defconst webjump-sample-sites
  57. '(
  58. ;; FSF, not including Emacs-specific.
  59. ("GNU Project FTP Archive" .
  60. ;; GNU FTP Mirror List from http://www.gnu.org/order/ftp.html
  61. [mirrors "ftp://ftp.gnu.org/pub/gnu/"
  62. "http://ftpmirror.gnu.org"])
  63. ("GNU Project Home Page" . "www.gnu.org")
  64. ;; Emacs.
  65. ("Emacs Home Page" .
  66. "www.gnu.org/software/emacs/emacs.html")
  67. ("Savannah Emacs page" .
  68. "savannah.gnu.org/projects/emacs")
  69. ("Emacs Lisp List" .
  70. "www.damtp.cam.ac.uk/user/eglen/emacs/ell.html")
  71. ("Emacs Wiki" .
  72. [simple-query "www.emacswiki.org"
  73. "www.emacswiki.org/cgi-bin/wiki/" ""])
  74. ;; Internet search engines.
  75. ("DuckDuckGo" .
  76. [simple-query "duckduckgo.com"
  77. "duckduckgo.com/?q=" ""])
  78. ("Google" .
  79. [simple-query "www.google.com"
  80. "www.google.com/search?q=" ""])
  81. ("Google Groups" .
  82. [simple-query "groups.google.com"
  83. "groups.google.com/groups?q=" ""])
  84. ("Yahoo" .
  85. [simple-query "www.yahoo.com" "search.yahoo.com/search?p=" ""])
  86. ("Yahoo: Reference" . "www.yahoo.com/Reference/")
  87. ("Wikipedia" .
  88. [simple-query "wikipedia.org" "wikipedia.org/wiki/" ""])
  89. ;; Misc. general interest.
  90. ("National Weather Service" . webjump-to-iwin)
  91. ("Usenet FAQs" .
  92. "www.faqs.org/faqs/")
  93. ("RTFM Usenet FAQs by Group" .
  94. "ftp://rtfm.mit.edu/pub/usenet-by-group/")
  95. ("RTFM Usenet FAQs by Hierarchy" .
  96. "ftp://rtfm.mit.edu/pub/usenet-by-hierarchy/")
  97. ("X Consortium Archive" . "ftp.x.org")
  98. ;; Computer social issues, privacy, professionalism.
  99. ("Association for Computing Machinery" . "www.acm.org")
  100. ("Computer Professionals for Social Responsibility" . "www.cpsr.org")
  101. ("Electronic Frontier Foundation" . "www.eff.org")
  102. ("IEEE Computer Society" . "www.computer.org")
  103. ("Risks Digest" . webjump-to-risks)
  104. ;; More.
  105. ("Supplemental Web site list for webjump" .
  106. "www.neilvandyke.org/webjump/")
  107. )
  108. "Sample hotlist for WebJump.
  109. See the documentation for `webjump' and `webjump-sites'.")
  110. (defconst webjump-state-to-postal-alist
  111. '(("Alabama" . "al") ("Alaska" . "ak") ("Arizona" . "az") ("Arkansas" . "ar")
  112. ("California" . "ca") ("Colorado" . "co") ("Connecticut" . "ct")
  113. ("Delaware" . "de") ("Florida" . "fl") ("Georgia" . "ga") ("Hawaii" . "hi")
  114. ("Idaho" . "id") ("Illinois" . "il") ("Indiana" . "in") ("Iowa" . "ia")
  115. ("Kansas" . "ks") ("Kentucky" . "ky") ("Louisiana" . "la") ("Maine" . "me")
  116. ("Maryland" . "md") ("Massachusetts" . "ma") ("Michigan" . "mi")
  117. ("Minnesota" . "mn") ("Mississippi" . "ms") ("Missouri" . "mo")
  118. ("Montana" . "mt") ("Nebraska" . "ne") ("Nevada" . "nv")
  119. ("New Hampshire" . "nh") ("New Jersey" . "nj") ("New Mexico" . "nm")
  120. ("New York" . "ny") ("North Carolina" . "nc") ("North Dakota" . "nd")
  121. ("Ohio" . "oh") ("Oklahoma" . "ok") ("Oregon" . "or")
  122. ("Pennsylvania" . "pa") ("Rhode Island" . "ri") ("South Carolina" . "sc")
  123. ("South Dakota" . "sd") ("Tennessee" . "tn") ("Texas" . "tx")
  124. ("Utah" . "ut") ("Vermont" . "vt") ("Virginia" . "va")
  125. ("Washington" . "wa") ("West Virginia" . "wv") ("Wisconsin" . "wi")
  126. ("Wyoming" . "wy")))
  127. ;;------------------------------------------------------------ Option Variables
  128. (defcustom webjump-sites webjump-sample-sites
  129. "Hotlist for WebJump.
  130. The hotlist is represented as an association list, with the CAR of each cell
  131. being the name of the Web site, and the CDR being the definition for the URL of
  132. that site. The URL definition can be a string (the URL), a vector (specifying
  133. a special \"builtin\" which returns a URL), a symbol (name of a function which
  134. returns a URL), or a list (which when `eval'ed yields a URL).
  135. If the URL definition is a vector, then a \"builtin\" is used. A builtin has a
  136. Lisp-like syntax, with the name as the first element of the vector, and any
  137. arguments as the following elements. The three current builtins are `name',
  138. which returns the name of the site as the URL, `simple-query', which
  139. returns a URL that is a function of a query entered by the user, and `mirrors',
  140. which allows the user to select from among multiple mirror sites for the same
  141. content.
  142. The first argument to the `simple-query' builtin is a static URL to use if the
  143. user enters a blank query. The second and third arguments are the prefix and
  144. suffix, respectively, to add to the encoded query the user enters. This
  145. builtin covers Web sites that have single-string searches with the query
  146. embedded in the URL.
  147. The arguments to the `mirrors' builtin are URLs of mirror sites.
  148. If the symbol of a function is given, then the function will be called with the
  149. Web site name (the one you specified in the CAR of the alist cell) as a
  150. parameter. This might come in handy for various kludges.
  151. For convenience, if the `http://', `ftp://', or `file://' prefix is missing
  152. from a URL, WebJump will make a guess at what you wanted and prepend it before
  153. submitting the URL."
  154. :type '(alist :key-type (string :tag "Name")
  155. :value-type (choice :tag "URL"
  156. (string :tag "URL")
  157. function
  158. (vector :tag "Builtin"
  159. (symbol :tag "Name")
  160. (repeat :inline t :tag "Arguments"
  161. string))
  162. (sexp :tag "Expression to eval"))))
  163. ;;------------------------------------------------------- Sample Site Functions
  164. (defun webjump-to-iwin (name)
  165. (let* ((prefix "http://www.nws.noaa.gov/view/")
  166. (state (webjump-read-choice name "state"
  167. (append '(("Puerto Rico" . "pr")
  168. ("Guam" . "gu")
  169. ("American Samoa" . "as")
  170. ("District of Columbia" . "dc")
  171. ("US Virgin Islands" . "vi"))
  172. webjump-state-to-postal-alist)))
  173. (opt (if state
  174. (webjump-read-choice
  175. name "option"
  176. '(("Hourly Report" . "hourly")
  177. ("State Forecast" . "state")
  178. ("Zone Forecast" . "zone")
  179. ("Short-Term Forecast" . "shortterm")
  180. ("Forecast Discussion" . "discussion")
  181. ("Weather Summary" . "summary")
  182. ("Public Information" . "public")
  183. ("Climatic Data" . "climate")
  184. ("Hydro Products" . "hydro")
  185. ("Watches" . "watches")
  186. ("Special Weather" . "special")
  187. ("Warnings and Advisories" . "warnings")
  188. ("Fire Weather" . "firewx"))))))
  189. (cond (opt (concat prefix "prodsByState.php?state=" state "&prodtype=" opt))
  190. (state (concat prefix "states.php?state=" state))
  191. (t prefix))))
  192. (defun webjump-to-risks (name)
  193. (let (issue volume)
  194. (if (and (setq volume (webjump-read-number (concat name " volume")))
  195. (setq issue (webjump-read-number (concat name " issue"))))
  196. (format "catless.ncl.ac.uk/Risks/%d.%02d.html" volume issue)
  197. "catless.ncl.ac.uk/Risks/")))
  198. ;;-------------------------------------------------------------- Core Functions
  199. ;;;###autoload
  200. (defun webjump ()
  201. "Jumps to a Web site from a programmable hotlist.
  202. See the documentation for the `webjump-sites' variable for how to customize the
  203. hotlist.
  204. Please submit bug reports and other feedback to the author, Neil W. Van Dyke
  205. <nwv@acm.org>."
  206. (interactive)
  207. (let* ((completion-ignore-case t)
  208. (item (assoc-string
  209. (completing-read "WebJump to site: " webjump-sites nil t)
  210. webjump-sites t))
  211. (name (car item))
  212. (expr (cdr item)))
  213. (browse-url (webjump-url-fix
  214. (cond ((not expr) "")
  215. ((stringp expr) expr)
  216. ((vectorp expr) (webjump-builtin expr name))
  217. ((listp expr) (eval expr))
  218. ((symbolp expr)
  219. (if (fboundp expr)
  220. (funcall expr name)
  221. (error "WebJump URL function \"%s\" undefined"
  222. expr)))
  223. (t (error "WebJump URL expression for \"%s\" invalid"
  224. name)))))))
  225. (defun webjump-builtin (expr name)
  226. (if (< (length expr) 1)
  227. (error "WebJump URL builtin for \"%s\" empty" name))
  228. (let ((builtin (aref expr 0)))
  229. (cond
  230. ((eq builtin 'mirrors)
  231. (if (= (length expr) 1)
  232. (error
  233. "WebJump URL builtin \"mirrors\" for \"%s\" needs at least 1 arg"
  234. name))
  235. (webjump-choose-mirror name (cdr (append expr nil))))
  236. ((eq builtin 'name)
  237. name)
  238. ((eq builtin 'simple-query)
  239. (webjump-builtin-check-args expr name 3)
  240. (webjump-do-simple-query name (aref expr 1) (aref expr 2) (aref expr 3)))
  241. (t (error "WebJump URL builtin \"%s\" for \"%s\" invalid"
  242. builtin name)))))
  243. (defun webjump-builtin-check-args (expr name count)
  244. (or (= (length expr) (1+ count))
  245. (error "WebJump URL builtin \"%s\" for \"%s\" needs %d args"
  246. (aref expr 0) name count)))
  247. (defun webjump-choose-mirror (name urls)
  248. (webjump-read-url-choice (concat name " mirror")
  249. urls
  250. (webjump-mirror-default urls)))
  251. (defun webjump-do-simple-query (name noquery-url query-prefix query-suffix)
  252. (let ((query (webjump-read-string (concat name " query"))))
  253. (if query
  254. (concat query-prefix (webjump-url-encode query) query-suffix)
  255. noquery-url)))
  256. (defun webjump-mirror-default (urls)
  257. ;; Note: This should be modified to apply some simple kludges/heuristics to
  258. ;; pick a site which is likely "close". As a tie-breaker among candidates
  259. ;; judged equally desirable, randomness might be used.
  260. (car urls))
  261. (defun webjump-read-choice (name what choices &optional default)
  262. (let* ((completion-ignore-case t)
  263. (choice (completing-read (concat name " " what ": ") choices nil t)))
  264. (if (webjump-null-or-blank-string-p choice)
  265. default
  266. (cdr (assoc choice choices)))))
  267. (defun webjump-read-number (prompt)
  268. ;; Note: I should make this more robust someday.
  269. (let ((input (webjump-read-string prompt)))
  270. (if input (string-to-number input))))
  271. (defun webjump-read-string (prompt)
  272. (let ((input (read-string (concat prompt ": "))))
  273. (if (webjump-null-or-blank-string-p input) nil input)))
  274. (defun webjump-read-url-choice (what urls &optional default)
  275. ;; Note: Convert this to use `webjump-read-choice' someday.
  276. (let* ((completions (mapcar (function (lambda (n) (cons n n)))
  277. urls))
  278. (input (completing-read (concat what
  279. ;;(if default " (RET for default)" "")
  280. ": ")
  281. completions
  282. nil
  283. t)))
  284. (if (webjump-null-or-blank-string-p input)
  285. default
  286. (car (assoc input completions)))))
  287. (defun webjump-null-or-blank-string-p (str)
  288. (or (null str) (string-match "^[ \t]*$" str)))
  289. (defun webjump-url-encode (str)
  290. (mapconcat (lambda (c)
  291. (let ((s (char-to-string c)))
  292. (cond ((string= s " ") "+")
  293. ((string-match "[a-zA-Z_.-/]" s) s)
  294. (t (upcase (format "%%%02x" c))))))
  295. (encode-coding-string str 'utf-8)
  296. ""))
  297. (defun webjump-url-fix (url)
  298. (if (webjump-null-or-blank-string-p url)
  299. ""
  300. (webjump-url-fix-trailing-slash
  301. (cond
  302. ((string-match "^[a-zA-Z]+:" url) url)
  303. ((string-match "^/" url) (concat "file://" url))
  304. ((string-match "^\\([^\\./]+\\)" url)
  305. (concat (if (string= (downcase (match-string 1 url)) "ftp")
  306. "ftp"
  307. "http")
  308. "://"
  309. url))
  310. (t url)))))
  311. (defun webjump-url-fix-trailing-slash (url)
  312. (if (string-match "^[a-zA-Z]+://[^/]+$" url)
  313. (concat url "/")
  314. url))
  315. ;;-----------------------------------------------------------------------------
  316. (provide 'webjump)
  317. ;;; webjump.el ends here