Commit | Line | Data |
---|---|---|
8749abea GM |
1 | ;;; goto-addr.el --- click to browse URL or to send to e-mail address |
2 | ||
cf6936a4 | 3 | ;; Copyright (C) 1995, 2000 Free Software Foundation, Inc. |
8749abea | 4 | |
cf6936a4 | 5 | ;; Author: Eric Ding <ericding@alum.mit.edu> |
12c74386 | 6 | ;; Maintainer: FSF |
8749abea GM |
7 | ;; Created: 15 Aug 1995 |
8 | ;; Keywords: mh-e, www, mouse, mail | |
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 2, or (at your option) | |
15 | ;; 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; see the file COPYING. If not, write to the | |
24 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 | ;; Boston, MA 02111-1307, USA. | |
26 | ||
27 | ;;; Commentary: | |
28 | ||
29 | ;; This package allows you to click or hit a key sequence while on a | |
30 | ;; URL or e-mail address, and either load the URL into a browser of | |
31 | ;; your choice using the browse-url package, or if it's an e-mail | |
32 | ;; address, to send an e-mail to that address. By default, we bind to | |
33 | ;; the [mouse-2] and the [C-c return] key sequences. | |
34 | ||
35 | ;; INSTALLATION | |
36 | ;; | |
37 | ;; To use goto-address in a particular mode (for example, while | |
38 | ;; reading mail in mh-e), add something like this in your .emacs file: | |
39 | ;; | |
40 | ;; (add-hook 'mh-show-mode-hook 'goto-address) | |
41 | ;; | |
28223a7e | 42 | ;; The mouse click method is bound to [mouse-2] on highlighted URLs or |
8749abea GM |
43 | ;; e-mail addresses only; it functions normally everywhere else. To bind |
44 | ;; another mouse click to the function, add the following to your .emacs | |
45 | ;; (for example): | |
46 | ;; | |
47 | ;; (setq goto-address-highlight-keymap | |
48 | ;; (let ((m (make-sparse-keymap))) | |
49 | ;; (define-key m [S-mouse-2] 'goto-address-at-mouse) | |
50 | ;; m)) | |
51 | ;; | |
52 | ||
53 | ;; BUG REPORTS | |
54 | ;; | |
55 | ;; Please send bug reports to me at ericding@mit.edu. | |
56 | ||
57 | ;; Known bugs/features: | |
58 | ;; * goto-address-mail-regexp only catches foo@bar.org style addressing, | |
59 | ;; not stuff like X.400 addresses, etc. | |
60 | ;; * regexp also catches Message-Id line, since it is in the format of | |
61 | ;; an Internet e-mail address (like Compuserve addresses) | |
62 | ;; * If show buffer is fontified after goto-address-fontify is run | |
63 | ;; (say, using font-lock-fontify-buffer), then font-lock face will | |
64 | ;; override goto-address faces. | |
65 | ||
66 | ;;; Code: | |
67 | ||
68 | (require 'browse-url) | |
69 | ||
70 | (defgroup goto-address nil | |
71 | "Click to browse URL or to send to e-mail address." | |
72 | :group 'mouse | |
73 | :group 'hypermedia) | |
74 | ||
75 | ||
76 | ;;; I don't expect users to want fontify'ing without highlighting. | |
77 | (defcustom goto-address-fontify-p t | |
28223a7e | 78 | "*If t, URLs and e-mail addresses in buffer are fontified. |
8749abea GM |
79 | But only if `goto-address-highlight-p' is also non-nil." |
80 | :type 'boolean | |
81 | :group 'goto-address) | |
82 | ||
83 | (defcustom goto-address-highlight-p t | |
28223a7e | 84 | "*If t, URLs and e-mail addresses in buffer are highlighted." |
8749abea GM |
85 | :type 'boolean |
86 | :group 'goto-address) | |
87 | ||
88 | (defcustom goto-address-fontify-maximum-size 30000 | |
28223a7e | 89 | "*Maximum size of file in which to fontify and/or highlight URLs." |
8749abea GM |
90 | :type 'integer |
91 | :group 'goto-address) | |
92 | ||
93 | (defvar goto-address-mail-regexp | |
94 | "[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" | |
95 | "A regular expression probably matching an e-mail address.") | |
96 | ||
97 | (defvar goto-address-url-regexp | |
98 | (concat "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|" | |
99 | "telnet\\|wais\\):\\(//[-a-zA-Z0-9_.]+:" | |
100 | "[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*" | |
101 | "[-a-zA-Z0-9_=#$@~`%&*+|\\/]") | |
102 | "A regular expression probably matching a URL.") | |
103 | ||
104 | (defvar goto-address-highlight-keymap | |
105 | (let ((m (make-sparse-keymap))) | |
106 | (define-key m [mouse-2] 'goto-address-at-mouse) | |
e38427c5 | 107 | (define-key m "\C-c\r" 'goto-address-at-point) |
8749abea GM |
108 | m) |
109 | "keymap to hold goto-addr's mouse key defs under highlighted URLs.") | |
110 | ||
111 | (defcustom goto-address-url-face 'bold | |
112 | "*Face to use for URLs." | |
113 | :type 'face | |
114 | :group 'goto-address) | |
115 | ||
116 | (defcustom goto-address-url-mouse-face 'highlight | |
117 | "*Face to use for URLs when the mouse is on them." | |
118 | :type 'face | |
119 | :group 'goto-address) | |
120 | ||
121 | (defcustom goto-address-mail-face 'italic | |
122 | "*Face to use for e-mail addresses." | |
123 | :type 'face | |
124 | :group 'goto-address) | |
125 | ||
126 | (defcustom goto-address-mail-mouse-face 'secondary-selection | |
127 | "*Face to use for e-mail addresses when the mouse is on them." | |
128 | :type 'face | |
129 | :group 'goto-address) | |
130 | ||
131 | (defun goto-address-fontify () | |
28223a7e | 132 | "Fontify the URLs and e-mail addresses in the current buffer. |
8749abea GM |
133 | This function implements `goto-address-highlight-p' |
134 | and `goto-address-fontify-p'." | |
135 | (save-excursion | |
136 | (let ((inhibit-read-only t) | |
137 | (inhibit-point-motion-hooks t) | |
138 | (modified (buffer-modified-p))) | |
139 | (goto-char (point-min)) | |
140 | (if (< (- (point-max) (point)) goto-address-fontify-maximum-size) | |
141 | (progn | |
142 | (while (re-search-forward goto-address-url-regexp nil t) | |
143 | (let* ((s (match-beginning 0)) | |
144 | (e (match-end 0)) | |
145 | (this-overlay (make-overlay s e))) | |
146 | (and goto-address-fontify-p | |
147 | (overlay-put this-overlay 'face goto-address-url-face)) | |
148 | (overlay-put this-overlay | |
149 | 'mouse-face goto-address-url-mouse-face) | |
fa59072a DL |
150 | (overlay-put this-overlay |
151 | 'help-echo "mouse-2: follow URL") | |
8749abea | 152 | (overlay-put this-overlay |
28223a7e | 153 | 'keymap goto-address-highlight-keymap))) |
8749abea GM |
154 | (goto-char (point-min)) |
155 | (while (re-search-forward goto-address-mail-regexp nil t) | |
156 | (let* ((s (match-beginning 0)) | |
157 | (e (match-end 0)) | |
158 | (this-overlay (make-overlay s e))) | |
159 | (and goto-address-fontify-p | |
160 | (overlay-put this-overlay 'face goto-address-mail-face)) | |
161 | (overlay-put this-overlay 'mouse-face | |
162 | goto-address-mail-mouse-face) | |
fa59072a DL |
163 | (overlay-put this-overlay |
164 | 'help-echo "mouse-2: follow URL") | |
8749abea | 165 | (overlay-put this-overlay |
28223a7e | 166 | 'keymap goto-address-highlight-keymap))))) |
8749abea GM |
167 | (and (buffer-modified-p) |
168 | (not modified) | |
169 | (set-buffer-modified-p nil))))) | |
170 | ||
171 | ;;; code to find and goto addresses; much of this has been blatantly | |
172 | ;;; snarfed from browse-url.el | |
173 | ||
174 | ;;;###autoload | |
175 | (defun goto-address-at-mouse (event) | |
176 | "Send to the e-mail address or load the URL clicked with the mouse. | |
177 | Send mail to address at position of mouse click. See documentation for | |
178 | `goto-address-find-address-at-point'. If no address is found | |
179 | there, then load the URL at or before the position of the mouse click." | |
180 | (interactive "e") | |
181 | (save-excursion | |
182 | (let ((posn (event-start event))) | |
183 | (set-buffer (window-buffer (posn-window posn))) | |
184 | (goto-char (posn-point posn)) | |
185 | (let ((address | |
186 | (save-excursion (goto-address-find-address-at-point)))) | |
187 | (if (string-equal address "") | |
188 | (let ((url (browse-url-url-at-point))) | |
189 | (if (string-equal url "") | |
190 | (error "No e-mail address or URL found") | |
191 | (browse-url url))) | |
192 | (compose-mail address)))))) | |
193 | ||
194 | ;;;###autoload | |
195 | (defun goto-address-at-point () | |
196 | "Send to the e-mail address or load the URL at point. | |
197 | Send mail to address at point. See documentation for | |
198 | `goto-address-find-address-at-point'. If no address is found | |
199 | there, then load the URL at or before point." | |
200 | (interactive) | |
201 | (save-excursion | |
202 | (let ((address (save-excursion (goto-address-find-address-at-point)))) | |
203 | (if (string-equal address "") | |
204 | (let ((url (browse-url-url-at-point))) | |
205 | (if (string-equal url "") | |
206 | (error "No e-mail address or URL found") | |
207 | (browse-url url))) | |
208 | (compose-mail address))))) | |
209 | ||
210 | (defun goto-address-find-address-at-point () | |
211 | "Find e-mail address around or before point. | |
212 | Then search backwards to beginning of line for the start of an e-mail | |
213 | address. If no e-mail address found, return the empty string." | |
214 | (let ((bol (save-excursion (beginning-of-line) (point)))) | |
215 | (re-search-backward "[^-_A-z0-9.@]" bol 'lim) | |
216 | (if (or (looking-at goto-address-mail-regexp) ; already at start | |
217 | (let ((eol (save-excursion (end-of-line) (point)))) | |
218 | (and (re-search-forward goto-address-mail-regexp eol 'lim) | |
219 | (goto-char (match-beginning 0))))) | |
220 | (buffer-substring (match-beginning 0) (match-end 0)) | |
221 | "")))m | |
222 | ||
223 | ;;;###autoload | |
224 | (defun goto-address () | |
225 | "Sets up goto-address functionality in the current buffer. | |
226 | Allows user to use mouse/keyboard command to click to go to a URL | |
227 | or to send e-mail. | |
228 | By default, goto-address binds to mouse-2 and C-c RET. | |
229 | ||
230 | Also fontifies the buffer appropriately (see `goto-address-fontify-p' and | |
231 | `goto-address-highlight-p' for more information)." | |
232 | (interactive) | |
8749abea GM |
233 | (if goto-address-highlight-p |
234 | (goto-address-fontify))) | |
235 | ||
236 | (provide 'goto-addr) | |
237 | ||
238 | ;;; goto-addr.el ends here. |