28210
|
1 ;;; goto-addr.el --- click to browse URL or to send to e-mail address
|
|
2
|
64701
|
3 ;; Copyright (C) 1995, 2000, 2001, 2002, 2003, 2004,
|
106815
|
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
|
28210
|
5
|
30192
|
6 ;; Author: Eric Ding <ericding@alum.mit.edu>
|
30285
|
7 ;; Maintainer: FSF
|
28210
|
8 ;; Created: 15 Aug 1995
|
|
9 ;; Keywords: mh-e, www, mouse, mail
|
|
10
|
|
11 ;; This file is part of GNU Emacs.
|
|
12
|
94677
|
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
|
28210
|
14 ;; it under the terms of the GNU General Public License as published by
|
94677
|
15 ;; the Free Software Foundation, either version 3 of the License, or
|
|
16 ;; (at your option) any later version.
|
28210
|
17
|
|
18 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 ;; GNU General Public License for more details.
|
|
22
|
|
23 ;; You should have received a copy of the GNU General Public License
|
94677
|
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
28210
|
25
|
|
26 ;;; Commentary:
|
|
27
|
|
28 ;; This package allows you to click or hit a key sequence while on a
|
|
29 ;; URL or e-mail address, and either load the URL into a browser of
|
|
30 ;; your choice using the browse-url package, or if it's an e-mail
|
|
31 ;; address, to send an e-mail to that address. By default, we bind to
|
|
32 ;; the [mouse-2] and the [C-c return] key sequences.
|
|
33
|
|
34 ;; INSTALLATION
|
|
35 ;;
|
|
36 ;; To use goto-address in a particular mode (for example, while
|
|
37 ;; reading mail in mh-e), add something like this in your .emacs file:
|
|
38 ;;
|
|
39 ;; (add-hook 'mh-show-mode-hook 'goto-address)
|
|
40 ;;
|
29664
|
41 ;; The mouse click method is bound to [mouse-2] on highlighted URLs or
|
28210
|
42 ;; e-mail addresses only; it functions normally everywhere else. To bind
|
|
43 ;; another mouse click to the function, add the following to your .emacs
|
|
44 ;; (for example):
|
|
45 ;;
|
|
46 ;; (setq goto-address-highlight-keymap
|
|
47 ;; (let ((m (make-sparse-keymap)))
|
62130
|
48 ;; (define-key m [S-mouse-2] 'goto-address-at-point)
|
28210
|
49 ;; m))
|
|
50 ;;
|
|
51
|
|
52 ;; Known bugs/features:
|
|
53 ;; * goto-address-mail-regexp only catches foo@bar.org style addressing,
|
|
54 ;; not stuff like X.400 addresses, etc.
|
|
55 ;; * regexp also catches Message-Id line, since it is in the format of
|
|
56 ;; an Internet e-mail address (like Compuserve addresses)
|
30345
|
57 ;; * If the buffer is fontified after goto-address-fontify is run
|
|
58 ;; (say, using font-lock-fontify-buffer), then font-lock faces will
|
28210
|
59 ;; override goto-address faces.
|
|
60
|
|
61 ;;; Code:
|
|
62
|
30891
|
63 (require 'thingatpt)
|
|
64 (autoload 'browse-url-url-at-point "browse-url")
|
28210
|
65
|
37423
|
66 ;; XEmacs needs the following definitions.
|
|
67 (unless (fboundp 'overlays-in)
|
|
68 (require 'overlay))
|
|
69 (unless (fboundp 'line-beginning-position)
|
|
70 (defalias 'line-beginning-position 'point-at-bol))
|
|
71 (unless (fboundp 'line-end-position)
|
|
72 (defalias 'line-end-position 'point-at-eol))
|
|
73 (unless (fboundp 'match-string-no-properties)
|
|
74 (defalias 'match-string-no-properties 'match-string))
|
|
75
|
28210
|
76 (defgroup goto-address nil
|
|
77 "Click to browse URL or to send to e-mail address."
|
|
78 :group 'mouse
|
107379
3c0cd76e49ff
Fix last change to use existing comm Custom group instead of `net'.
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
79 :group 'comm)
|
28210
|
80
|
|
81
|
39568
|
82 ;; I don't expect users to want fontify'ing without highlighting.
|
28210
|
83 (defcustom goto-address-fontify-p t
|
30891
|
84 "*Non-nil means URLs and e-mail addresses in buffer are fontified.
|
28210
|
85 But only if `goto-address-highlight-p' is also non-nil."
|
|
86 :type 'boolean
|
|
87 :group 'goto-address)
|
|
88
|
|
89 (defcustom goto-address-highlight-p t
|
30891
|
90 "*Non-nil means URLs and e-mail addresses in buffer are highlighted."
|
28210
|
91 :type 'boolean
|
|
92 :group 'goto-address)
|
|
93
|
|
94 (defcustom goto-address-fontify-maximum-size 30000
|
51098
|
95 "*Maximum size of file in which to fontify and/or highlight URLs.
|
|
96 A value of t means there is no limit--fontify regardless of the size."
|
|
97 :type '(choice (integer :tag "Maximum size") (const :tag "No limit" t))
|
28210
|
98 :group 'goto-address)
|
|
99
|
|
100 (defvar goto-address-mail-regexp
|
39790
|
101 ;; Actually pretty much any char could appear in the username part. -stef
|
59117
|
102 "[-a-zA-Z0-9=._+]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+"
|
28210
|
103 "A regular expression probably matching an e-mail address.")
|
|
104
|
48068
|
105 (defvar goto-address-url-regexp
|
66695
0ade01371c2e
(goto-address-url-regexp): Remove `data:' URLs from goto-address-url-regexp.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
106 (concat
|
0ade01371c2e
(goto-address-url-regexp): Remove `data:' URLs from goto-address-url-regexp.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
107 "\\<\\("
|
0ade01371c2e
(goto-address-url-regexp): Remove `data:' URLs from goto-address-url-regexp.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
108 (mapconcat 'identity
|
0ade01371c2e
(goto-address-url-regexp): Remove `data:' URLs from goto-address-url-regexp.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
109 (delete "mailto:"
|
0ade01371c2e
(goto-address-url-regexp): Remove `data:' URLs from goto-address-url-regexp.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
110 ;; Remove `data:', as it's not terribly useful to follow
|
0ade01371c2e
(goto-address-url-regexp): Remove `data:' URLs from goto-address-url-regexp.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
111 ;; those. Leaving them causes `use Data::Dumper;' to be
|
0ade01371c2e
(goto-address-url-regexp): Remove `data:' URLs from goto-address-url-regexp.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
112 ;; fontified oddly in Perl files.
|
0ade01371c2e
(goto-address-url-regexp): Remove `data:' URLs from goto-address-url-regexp.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
113 (delete "data:"
|
0ade01371c2e
(goto-address-url-regexp): Remove `data:' URLs from goto-address-url-regexp.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
114 (copy-sequence thing-at-point-uri-schemes)))
|
0ade01371c2e
(goto-address-url-regexp): Remove `data:' URLs from goto-address-url-regexp.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
115 "\\|")
|
0ade01371c2e
(goto-address-url-regexp): Remove `data:' URLs from goto-address-url-regexp.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
116 "\\)"
|
0ade01371c2e
(goto-address-url-regexp): Remove `data:' URLs from goto-address-url-regexp.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
117 thing-at-point-url-path-regexp)
|
39568
|
118 ;; (concat "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|"
|
|
119 ;; "telnet\\|wais\\):\\(//[-a-zA-Z0-9_.]+:"
|
|
120 ;; "[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*"
|
|
121 ;; "[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
|
28210
|
122 "A regular expression probably matching a URL.")
|
|
123
|
|
124 (defvar goto-address-highlight-keymap
|
|
125 (let ((m (make-sparse-keymap)))
|
62130
|
126 (define-key m (if (featurep 'xemacs) (kbd "<button2>") (kbd "<mouse-2>"))
|
62131
|
127 'goto-address-at-point)
|
37423
|
128 (define-key m (kbd "C-c RET") 'goto-address-at-point)
|
28210
|
129 m)
|
67914
|
130 "Keymap to hold goto-addr's mouse key defs under highlighted URLs.")
|
28210
|
131
|
101184
8b936847a612
(goto-address-url-face): Change default value from `bold' to `link'.
Juri Linkov <juri@jurta.org>
diff
changeset
|
132 (defcustom goto-address-url-face 'link
|
30891
|
133 "Face to use for URLs."
|
28210
|
134 :type 'face
|
|
135 :group 'goto-address)
|
|
136
|
|
137 (defcustom goto-address-url-mouse-face 'highlight
|
30891
|
138 "Face to use for URLs when the mouse is on them."
|
28210
|
139 :type 'face
|
|
140 :group 'goto-address)
|
|
141
|
|
142 (defcustom goto-address-mail-face 'italic
|
30891
|
143 "Face to use for e-mail addresses."
|
28210
|
144 :type 'face
|
|
145 :group 'goto-address)
|
|
146
|
|
147 (defcustom goto-address-mail-mouse-face 'secondary-selection
|
30891
|
148 "Face to use for e-mail addresses when the mouse is on them."
|
28210
|
149 :type 'face
|
|
150 :group 'goto-address)
|
|
151
|
94318
|
152 (defun goto-address-unfontify (start end)
|
|
153 "Remove `goto-address' fontification from the given region."
|
|
154 (dolist (overlay (overlays-in start end))
|
|
155 (if (overlay-get overlay 'goto-address)
|
|
156 (delete-overlay overlay))))
|
|
157
|
94325
|
158 (defvar goto-address-prog-mode)
|
|
159
|
28210
|
160 (defun goto-address-fontify ()
|
29664
|
161 "Fontify the URLs and e-mail addresses in the current buffer.
|
28210
|
162 This function implements `goto-address-highlight-p'
|
|
163 and `goto-address-fontify-p'."
|
30345
|
164 ;; Clean up from any previous go.
|
94318
|
165 (goto-address-unfontify (point-min) (point-max))
|
28210
|
166 (save-excursion
|
30345
|
167 (let ((inhibit-point-motion-hooks t))
|
28210
|
168 (goto-char (point-min))
|
94318
|
169 (when (or (eq t goto-address-fontify-maximum-size)
|
|
170 (< (- (point-max) (point)) goto-address-fontify-maximum-size))
|
|
171 (while (re-search-forward goto-address-url-regexp nil t)
|
|
172 (let* ((s (match-beginning 0))
|
|
173 (e (match-end 0))
|
|
174 this-overlay)
|
|
175 (when (or (not goto-address-prog-mode)
|
|
176 ;; This tests for both comment and string
|
|
177 ;; syntax.
|
|
178 (nth 8 (syntax-ppss)))
|
|
179 (setq this-overlay (make-overlay s e))
|
|
180 (and goto-address-fontify-p
|
|
181 (overlay-put this-overlay 'face goto-address-url-face))
|
|
182 (overlay-put this-overlay 'evaporate t)
|
|
183 (overlay-put this-overlay
|
|
184 'mouse-face goto-address-url-mouse-face)
|
|
185 (overlay-put this-overlay 'follow-link t)
|
|
186 (overlay-put this-overlay
|
|
187 'help-echo "mouse-2, C-c RET: follow URL")
|
|
188 (overlay-put this-overlay
|
|
189 'keymap goto-address-highlight-keymap)
|
|
190 (overlay-put this-overlay 'goto-address t))))
|
|
191 (goto-char (point-min))
|
|
192 (while (re-search-forward goto-address-mail-regexp nil t)
|
|
193 (let* ((s (match-beginning 0))
|
|
194 (e (match-end 0))
|
|
195 this-overlay)
|
|
196 (when (or (not goto-address-prog-mode)
|
|
197 ;; This tests for both comment and string
|
|
198 ;; syntax.
|
|
199 (nth 8 (syntax-ppss)))
|
|
200 (setq this-overlay (make-overlay s e))
|
|
201 (and goto-address-fontify-p
|
|
202 (overlay-put this-overlay 'face goto-address-mail-face))
|
|
203 (overlay-put this-overlay 'evaporate t)
|
|
204 (overlay-put this-overlay 'mouse-face
|
|
205 goto-address-mail-mouse-face)
|
|
206 (overlay-put this-overlay 'follow-link t)
|
|
207 (overlay-put this-overlay
|
|
208 'help-echo "mouse-2, C-c RET: mail this address")
|
|
209 (overlay-put this-overlay
|
|
210 'keymap goto-address-highlight-keymap)
|
|
211 (overlay-put this-overlay 'goto-address t))))))))
|
|
212
|
|
213 (defun goto-address-fontify-region (start end)
|
|
214 "Fontify URLs and e-mail addresses in the given region."
|
|
215 (save-excursion
|
|
216 (save-restriction
|
|
217 (let ((beg-line (progn (goto-char start) (line-beginning-position)))
|
|
218 (end-line (progn (goto-char end) (line-end-position))))
|
|
219 (narrow-to-region beg-line end-line)
|
|
220 (goto-address-fontify)))))
|
28210
|
221
|
39568
|
222 ;; code to find and goto addresses; much of this has been blatantly
|
|
223 ;; snarfed from browse-url.el
|
28210
|
224
|
|
225 ;;;###autoload
|
62130
|
226 (define-obsolete-function-alias
|
|
227 'goto-address-at-mouse 'goto-address-at-point "22.1")
|
28210
|
228
|
|
229 ;;;###autoload
|
62130
|
230 (defun goto-address-at-point (&optional event)
|
28210
|
231 "Send to the e-mail address or load the URL at point.
|
|
232 Send mail to address at point. See documentation for
|
|
233 `goto-address-find-address-at-point'. If no address is found
|
|
234 there, then load the URL at or before point."
|
62130
|
235 (interactive (list last-input-event))
|
28210
|
236 (save-excursion
|
67083
78a5a90bd7ec
(goto-address-fontify): Put `follow-link' property on mail and url overlays.
John Paul Wallington <jpw@pobox.com>
diff
changeset
|
237 (if event (posn-set-point (event-end event)))
|
28210
|
238 (let ((address (save-excursion (goto-address-find-address-at-point))))
|
49598
|
239 (if (and address
|
37809
|
240 (save-excursion
|
|
241 (goto-char (previous-single-char-property-change
|
|
242 (point) 'goto-address nil
|
|
243 (line-beginning-position)))
|
|
244 (not (looking-at goto-address-url-regexp))))
|
30891
|
245 (compose-mail address)
|
|
246 (let ((url (browse-url-url-at-point)))
|
|
247 (if url
|
|
248 (browse-url url)
|
|
249 (error "No e-mail address or URL found")))))))
|
28210
|
250
|
|
251 (defun goto-address-find-address-at-point ()
|
|
252 "Find e-mail address around or before point.
|
|
253 Then search backwards to beginning of line for the start of an e-mail
|
30891
|
254 address. If no e-mail address found, return nil."
|
|
255 (re-search-backward "[^-_A-z0-9.@]" (line-beginning-position) 'lim)
|
|
256 (if (or (looking-at goto-address-mail-regexp) ; already at start
|
|
257 (and (re-search-forward goto-address-mail-regexp
|
|
258 (line-end-position) 'lim)
|
|
259 (goto-char (match-beginning 0))))
|
|
260 (match-string-no-properties 0)))
|
28210
|
261
|
|
262 ;;;###autoload
|
|
263 (defun goto-address ()
|
|
264 "Sets up goto-address functionality in the current buffer.
|
|
265 Allows user to use mouse/keyboard command to click to go to a URL
|
|
266 or to send e-mail.
|
67914
|
267 By default, goto-address binds `goto-address-at-point' to mouse-2 and C-c RET
|
|
268 only on URLs and e-mail addresses.
|
28210
|
269
|
|
270 Also fontifies the buffer appropriately (see `goto-address-fontify-p' and
|
|
271 `goto-address-highlight-p' for more information)."
|
|
272 (interactive)
|
|
273 (if goto-address-highlight-p
|
|
274 (goto-address-fontify)))
|
71636
0fa81d575f41
* net/goto-addr.el (goto-address): Mark as safe for local evals.
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
275 ;;;###autoload(put 'goto-address 'safe-local-eval-function t)
|
28210
|
276
|
94318
|
277 ;;;###autoload
|
|
278 (define-minor-mode goto-address-mode
|
|
279 "Minor mode to buttonize URLs and e-mail addresses in the current buffer."
|
|
280 nil
|
|
281 ""
|
|
282 nil
|
|
283 (if goto-address-mode
|
|
284 (jit-lock-register #'goto-address-fontify-region)
|
|
285 (jit-lock-unregister #'goto-address-fontify-region)
|
|
286 (save-restriction
|
|
287 (widen)
|
|
288 (goto-address-unfontify (point-min) (point-max)))))
|
|
289
|
|
290 ;;;###autoload
|
|
291 (define-minor-mode goto-address-prog-mode
|
|
292 "Turn on `goto-address-mode', but only in comments and strings."
|
|
293 nil
|
|
294 ""
|
|
295 nil
|
|
296 (if goto-address-prog-mode
|
|
297 (jit-lock-register #'goto-address-fontify-region)
|
|
298 (jit-lock-unregister #'goto-address-fontify-region)
|
|
299 (save-restriction
|
|
300 (widen)
|
|
301 (goto-address-unfontify (point-min) (point-max)))))
|
|
302
|
28210
|
303 (provide 'goto-addr)
|
|
304
|
62130
|
305 ;; arch-tag: ca47c505-5661-425d-a471-62bc6e75cf0a
|
38412
|
306 ;;; goto-addr.el ends here
|