Add COPYING file; update headers.
[muse-el.git] / lisp / muse-protocols.el
blobd12aad73fab27df5af31e1046d8b430c37a97541
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 (defcustom muse-wikipedia-country "en"
105 "Indicate the 2-digit country code that we use for Wikipedia
106 queries."
107 :type 'string
108 :options '("de" "en" "es" "fr" "it" "pl" "pt" "ja" "nl" "sv")
109 :group 'muse)
111 (defun muse-protocol-find (proto list)
112 "Return the first element of LIST whose car matches the regexp PROTO."
113 (setq list (copy-alist list))
114 (let (entry)
115 (while list
116 (when (string-match (caar list) proto)
117 (setq entry (car list)
118 list nil))
119 (setq list (cdr list)))
120 entry))
122 ;;;###autoload
123 (defun muse-browse-url (url &optional other-window)
124 "Handle URL with the function specified in `muse-url-protocols'.
125 If OTHER-WINDOW is non-nil, open in a different window."
126 (interactive (list (read-string "URL: ")
127 current-prefix-arg))
128 ;; Strip text properties
129 (when (fboundp 'set-text-properties)
130 (set-text-properties 0 (length url) nil url))
131 (when other-window
132 (switch-to-buffer-other-window (current-buffer)))
133 (when (string-match muse-url-regexp url)
134 (let* ((proto (concat "\\`" (match-string 1 url)))
135 (entry (muse-protocol-find proto muse-url-protocols)))
136 (when entry
137 (funcall (cadr entry) url)))))
139 (defun muse-resolve-url (url &rest ignored)
140 "Resolve URL with the function specified in `muse-url-protocols'."
141 (when (string-match muse-url-regexp url)
142 (let* ((proto (concat "\\`" (match-string 1 url)))
143 (entry (muse-protocol-find proto muse-url-protocols)))
144 (when entry
145 (let ((func (car (cddr entry))))
146 (if func
147 (setq url (funcall func url))
148 (setq url nil))))))
149 url)
151 (defun muse-protocol-add (protocol browse-function resolve-function)
152 "Add PROTOCOL to `muse-url-protocols'. PROTOCOL may be a regexp.
154 BROWSE-FUNCTION should be a function that visits a URL in the
155 current buffer.
157 RESOLVE-FUNCTION should be a function that transforms a URL for
158 publishing or returns nil if not linked."
159 (add-to-list 'muse-url-protocols
160 (list protocol browse-function resolve-function))
161 (muse-update-url-regexp 'muse-url-protocols
162 muse-url-protocols))
164 (defun muse-resolve-url-dict (url)
165 "Return the Wikipedia link corresponding with the given URL."
166 (when (string-match "\\`dict:\\(.+\\)" url)
167 (concat "http://" muse-wikipedia-country ".wikipedia.org/"
168 "wiki/Special:Search?search=" (match-string 1 url))))
170 (defun muse-browse-url-dict (url)
171 "If this is a Wikipedia URL, browse it."
172 (let ((dict-url (muse-resolve-url-dict url)))
173 (when dict-url
174 (browse-url dict-url))))
176 (defun muse-resolve-url-doi (url)
177 "Return the URL through DOI proxy server."
178 (when (string-match "\\`doi:\\(.+\\)" url)
179 (concat "http://dx.doi.org/"
180 (match-string 1 url))))
182 (defun muse-browse-url-doi (url)
183 "If this is a DOI URL, browse it.
185 DOI's (digitial object identifiers) are a standard identifier
186 used in the publishing industry."
187 (let ((doi-url (muse-resolve-url-doi url)))
188 (when doi-url
189 (browse-url doi-url))))
191 (defun muse-resolve-url-google (url)
192 "Return the correct Google search string."
193 (when (string-match "\\`google:/?/?\\(.+\\)" url)
194 (concat "http://www.google.com/search?q="
195 (match-string 1 url))))
197 (defun muse-browse-url-google (url)
198 "If this is a Google URL, jump to it."
199 (let ((google-url (muse-resolve-url-google url)))
200 (when google-url
201 (browse-url google-url))))
203 (defun muse-browse-url-info (url)
204 "If this in an Info URL, jump to it."
205 (require 'info)
206 (cond
207 ((string-match "\\`info://\\([^#\n]+\\)#\\(.+\\)" url)
208 (Info-find-node (match-string 1 url)
209 (match-string 2 url)))
210 ((string-match "\\`info://\\([^#\n]+\\)" url)
211 (Info-find-node (match-string 1 url)
212 "Top"))
213 ((string-match "\\`info://(\\([^)\n]+\\))\\(.+\\)" url)
214 (Info-find-node (match-string 1 url) (match-string 2 url)))
215 ((string-match "\\`info://\\(.+\\)" url)
216 (Info-find-node (match-string 1 url) "Top"))))
218 (defun muse-browse-url-man (url)
219 "If this in a manpage URL, jump to it."
220 (cond ((string-match "\\`man://\\(.+\\):\\(.+\\)" url)
221 (manual-entry (concat (match-string 1 url)
222 "(" (match-string 2 url) ")")))
223 ((string-match "\\`man://\\(.+\\)" url)
224 (manual-entry (concat (match-string 1 url))))))
226 (provide 'muse-protocols)
228 ;;; muse-protocols.el ends here