Merged from mwolson@gnu.org--2006-muse-el (patch 92)
[muse-el.git] / lisp / muse-protocols.el
blob7d17c02c589d70657a19f0c92da5fb6fe3947232
1 ;;; muse-protocols.el --- URL protocols that Muse recognizes
3 ;; Copyright (C) 2005, 2006 Free Software Foundation, Inc.
5 ;; Author: Brad Collins (brad AT chenla DOT org)
7 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
9 ;; Emacs Muse is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
14 ;; Emacs Muse is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with Emacs Muse; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
24 ;;; Commentary:
26 ;; Here's an example for adding a protocol for the site yubnub, a Web
27 ;; Command line service.
29 ;; (add-to-list 'muse-url-protocols '("yubnub://" muse-browse-url-yubnub
30 ;; muse-resolve-url-yubnub))
32 ;; (defun muse-resolve-url-yubnub (url)
33 ;; "Resolve a yubnub URL."
34 ;; ;; Remove the yubnub://
35 ;; (when (string-match "\\`yubnub://\\(.+\\)" url)
36 ;; (match-string 1)))
38 ;; (defun muse-browse-url-yubnub (url)
39 ;; "If this is a yubnub URL-command, jump to it."
40 ;; (setq url (muse-resolve-url-yubnub url))
41 ;; (browse-url (concat "http://yubnub.org/parser/parse?command="
42 ;; url)))
44 ;;; Contributors:
46 ;; Phillip Lord (Phillip.Lord AT newcastle DOT ac DOT uk) provided a
47 ;; handler for DOI URLs.
49 ;;; Code:
51 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
53 ;; Muse URL Protocols
55 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
57 (require 'info)
58 (require 'muse-regexps)
60 (defvar muse-url-regexp nil
61 "A regexp used to match URLs within a Muse page.
62 This is autogenerated from `muse-url-protocols'.")
64 (defun muse-update-url-regexp (sym value)
65 (setq muse-url-regexp
66 (concat "\\<\\(" (mapconcat 'car value "\\|") "\\)"
67 "[^][" muse-regexp-blank "\"'()<>^`{}\n]*"
68 "[^][" muse-regexp-blank "\"'()<>^`{}.,;\n]+"))
69 (set sym value))
71 (defcustom muse-url-protocols
72 '(("info://" muse-browse-url-info nil)
73 ("man://" muse-browse-url-man nil)
74 ("google://" muse-browse-url-google muse-resolve-url-google)
75 ("http:/?/?" browse-url identity)
76 ("https:/?/?" browse-url identity)
77 ("ftp:/?/?" browse-url identity)
78 ("gopher://" browse-url identity)
79 ("telnet://" browse-url identity)
80 ("wais://" browse-url identity)
81 ("file://?" browse-url identity)
82 ("dict:" muse-browse-url-dict muse-resolve-url-dict)
83 ("doi:" muse-browse-url-doi muse-resolve-url-doi)
84 ("news:" browse-url identity)
85 ("snews:" browse-url identity)
86 ("mailto:" browse-url identity))
87 "A list of (PROTOCOL BROWSE-FUN RESOLVE-FUN) used to match URL protocols.
88 PROTOCOL describes the first part of the URL, including the
89 \"://\" part. This may be a regexp.
91 BROWSE-FUN should accept URL as an argument and open the URL in
92 the current window.
94 RESOLVE-FUN should accept URL as an argument and return the final
95 URL, or nil if no URL should be included."
96 :type '(repeat (list :tag "Protocol"
97 (string :tag "Regexp")
98 (function :tag "Browse")
99 (choice (function :tag "Resolve")
100 (const :tag "Don't resolve" nil))))
101 :set 'muse-update-url-regexp
102 :group 'muse)
104 (add-hook 'muse-update-values-hook
105 (lambda ()
106 (muse-update-url-regexp 'muse-url-protocols muse-url-protocols)))
108 (defcustom muse-wikipedia-country "en"
109 "Indicate the 2-digit country code that we use for Wikipedia
110 queries."
111 :type 'string
112 :options '("de" "en" "es" "fr" "it" "pl" "pt" "ja" "nl" "sv")
113 :group 'muse)
115 (defun muse-protocol-find (proto list)
116 "Return the first element of LIST whose car matches the regexp PROTO."
117 (setq list (copy-alist list))
118 (let (entry)
119 (while list
120 (when (string-match (caar list) proto)
121 (setq entry (car list)
122 list nil))
123 (setq list (cdr list)))
124 entry))
126 ;;;###autoload
127 (defun muse-browse-url (url &optional other-window)
128 "Handle URL with the function specified in `muse-url-protocols'.
129 If OTHER-WINDOW is non-nil, open in a different window."
130 (interactive (list (read-string "URL: ")
131 current-prefix-arg))
132 ;; Strip text properties
133 (when (fboundp 'set-text-properties)
134 (set-text-properties 0 (length url) nil url))
135 (when other-window
136 (switch-to-buffer-other-window (current-buffer)))
137 (when (string-match muse-url-regexp url)
138 (let* ((proto (concat "\\`" (match-string 1 url)))
139 (entry (muse-protocol-find proto muse-url-protocols)))
140 (when entry
141 (funcall (cadr entry) url)))))
143 (defun muse-resolve-url (url &rest ignored)
144 "Resolve URL with the function specified in `muse-url-protocols'."
145 (when (string-match muse-url-regexp url)
146 (let* ((proto (concat "\\`" (match-string 1 url)))
147 (entry (muse-protocol-find proto muse-url-protocols)))
148 (when entry
149 (let ((func (car (cddr entry))))
150 (if func
151 (setq url (funcall func url))
152 (setq url nil))))))
153 url)
155 (defun muse-protocol-add (protocol browse-function resolve-function)
156 "Add PROTOCOL to `muse-url-protocols'. PROTOCOL may be a regexp.
158 BROWSE-FUNCTION should be a function that visits a URL in the
159 current buffer.
161 RESOLVE-FUNCTION should be a function that transforms a URL for
162 publishing or returns nil if not linked."
163 (add-to-list 'muse-url-protocols
164 (list protocol browse-function resolve-function))
165 (muse-update-url-regexp 'muse-url-protocols
166 muse-url-protocols))
168 (defun muse-resolve-url-dict (url)
169 "Return the Wikipedia link corresponding with the given URL."
170 (when (string-match "\\`dict:\\(.+\\)" url)
171 (concat "http://" muse-wikipedia-country ".wikipedia.org/"
172 "wiki/Special:Search?search=" (match-string 1 url))))
174 (defun muse-browse-url-dict (url)
175 "If this is a Wikipedia URL, browse it."
176 (let ((dict-url (muse-resolve-url-dict url)))
177 (when dict-url
178 (browse-url dict-url))))
180 (defun muse-resolve-url-doi (url)
181 "Return the URL through DOI proxy server."
182 (when (string-match "\\`doi:\\(.+\\)" url)
183 (concat "http://dx.doi.org/"
184 (match-string 1 url))))
186 (defun muse-browse-url-doi (url)
187 "If this is a DOI URL, browse it.
189 DOI's (digitial object identifiers) are a standard identifier
190 used in the publishing industry."
191 (let ((doi-url (muse-resolve-url-doi url)))
192 (when doi-url
193 (browse-url doi-url))))
195 (defun muse-resolve-url-google (url)
196 "Return the correct Google search string."
197 (when (string-match "\\`google:/?/?\\(.+\\)" url)
198 (concat "http://www.google.com/search?q="
199 (match-string 1 url))))
201 (defun muse-browse-url-google (url)
202 "If this is a Google URL, jump to it."
203 (let ((google-url (muse-resolve-url-google url)))
204 (when google-url
205 (browse-url google-url))))
207 (defun muse-browse-url-info (url)
208 "If this in an Info URL, jump to it."
209 (require 'info)
210 (cond
211 ((string-match "\\`info://\\([^#\n]+\\)#\\(.+\\)" url)
212 (Info-find-node (match-string 1 url)
213 (match-string 2 url)))
214 ((string-match "\\`info://\\([^#\n]+\\)" url)
215 (Info-find-node (match-string 1 url)
216 "Top"))
217 ((string-match "\\`info://(\\([^)\n]+\\))\\(.+\\)" url)
218 (Info-find-node (match-string 1 url) (match-string 2 url)))
219 ((string-match "\\`info://\\(.+\\)" url)
220 (Info-find-node (match-string 1 url) "Top"))))
222 (defun muse-browse-url-man (url)
223 "If this in a manpage URL, jump to it."
224 (cond ((string-match "\\`man://\\(.+\\):\\(.+\\)" url)
225 (manual-entry (concat (match-string 1 url)
226 "(" (match-string 2 url) ")")))
227 ((string-match "\\`man://\\(.+\\)" url)
228 (manual-entry (concat (match-string 1 url))))))
230 (provide 'muse-protocols)
232 ;;; muse-protocols.el ends here