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