Mercurial > emacs
annotate lisp/net/goto-addr.el @ 30378:54d7faf67340
*** empty log message ***
author | Miles Bader <miles@gnu.org> |
---|---|
date | Sat, 22 Jul 2000 13:36:08 +0000 |
parents | d24028985f3d |
children | 42ce5997aa4a |
rev | line source |
---|---|
28210 | 1 ;;; goto-addr.el --- click to browse URL or to send to e-mail address |
2 | |
30192 | 3 ;; Copyright (C) 1995, 2000 Free Software Foundation, Inc. |
28210 | 4 |
30192 | 5 ;; Author: Eric Ding <ericding@alum.mit.edu> |
30285 | 6 ;; Maintainer: FSF |
28210 | 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 ;; | |
29664
3e03d72fadea
(goto-address-fontify): Use keymap property, not local-map.
Dave Love <fx@gnu.org>
parents:
29653
diff
changeset
|
42 ;; The mouse click method is bound to [mouse-2] on highlighted URLs or |
28210 | 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 ;; Known bugs/features: | |
54 ;; * goto-address-mail-regexp only catches foo@bar.org style addressing, | |
55 ;; not stuff like X.400 addresses, etc. | |
56 ;; * regexp also catches Message-Id line, since it is in the format of | |
57 ;; an Internet e-mail address (like Compuserve addresses) | |
30345
d24028985f3d
(goto-address-fontify): Don't bother with buffer-modified and read-only
Dave Love <fx@gnu.org>
parents:
30285
diff
changeset
|
58 ;; * If the buffer is fontified after goto-address-fontify is run |
d24028985f3d
(goto-address-fontify): Don't bother with buffer-modified and read-only
Dave Love <fx@gnu.org>
parents:
30285
diff
changeset
|
59 ;; (say, using font-lock-fontify-buffer), then font-lock faces will |
28210 | 60 ;; override goto-address faces. |
61 | |
62 ;;; Code: | |
63 | |
64 (require 'browse-url) | |
65 | |
66 (defgroup goto-address nil | |
67 "Click to browse URL or to send to e-mail address." | |
68 :group 'mouse | |
69 :group 'hypermedia) | |
70 | |
71 | |
72 ;;; I don't expect users to want fontify'ing without highlighting. | |
73 (defcustom goto-address-fontify-p t | |
29664
3e03d72fadea
(goto-address-fontify): Use keymap property, not local-map.
Dave Love <fx@gnu.org>
parents:
29653
diff
changeset
|
74 "*If t, URLs and e-mail addresses in buffer are fontified. |
28210 | 75 But only if `goto-address-highlight-p' is also non-nil." |
76 :type 'boolean | |
77 :group 'goto-address) | |
78 | |
79 (defcustom goto-address-highlight-p t | |
29664
3e03d72fadea
(goto-address-fontify): Use keymap property, not local-map.
Dave Love <fx@gnu.org>
parents:
29653
diff
changeset
|
80 "*If t, URLs and e-mail addresses in buffer are highlighted." |
28210 | 81 :type 'boolean |
82 :group 'goto-address) | |
83 | |
84 (defcustom goto-address-fontify-maximum-size 30000 | |
29664
3e03d72fadea
(goto-address-fontify): Use keymap property, not local-map.
Dave Love <fx@gnu.org>
parents:
29653
diff
changeset
|
85 "*Maximum size of file in which to fontify and/or highlight URLs." |
28210 | 86 :type 'integer |
87 :group 'goto-address) | |
88 | |
89 (defvar goto-address-mail-regexp | |
90 "[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" | |
91 "A regular expression probably matching an e-mail address.") | |
92 | |
93 (defvar goto-address-url-regexp | |
94 (concat "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|" | |
95 "telnet\\|wais\\):\\(//[-a-zA-Z0-9_.]+:" | |
96 "[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*" | |
97 "[-a-zA-Z0-9_=#$@~`%&*+|\\/]") | |
98 "A regular expression probably matching a URL.") | |
99 | |
100 (defvar goto-address-highlight-keymap | |
101 (let ((m (make-sparse-keymap))) | |
102 (define-key m [mouse-2] 'goto-address-at-mouse) | |
29653
dfb2dd2b862a
(goto-address): Don't bind C-c RET locally.
Gerd Moellmann <gerd@gnu.org>
parents:
29209
diff
changeset
|
103 (define-key m "\C-c\r" 'goto-address-at-point) |
28210 | 104 m) |
105 "keymap to hold goto-addr's mouse key defs under highlighted URLs.") | |
106 | |
107 (defcustom goto-address-url-face 'bold | |
108 "*Face to use for URLs." | |
109 :type 'face | |
110 :group 'goto-address) | |
111 | |
112 (defcustom goto-address-url-mouse-face 'highlight | |
113 "*Face to use for URLs when the mouse is on them." | |
114 :type 'face | |
115 :group 'goto-address) | |
116 | |
117 (defcustom goto-address-mail-face 'italic | |
118 "*Face to use for e-mail addresses." | |
119 :type 'face | |
120 :group 'goto-address) | |
121 | |
122 (defcustom goto-address-mail-mouse-face 'secondary-selection | |
123 "*Face to use for e-mail addresses when the mouse is on them." | |
124 :type 'face | |
125 :group 'goto-address) | |
126 | |
127 (defun goto-address-fontify () | |
29664
3e03d72fadea
(goto-address-fontify): Use keymap property, not local-map.
Dave Love <fx@gnu.org>
parents:
29653
diff
changeset
|
128 "Fontify the URLs and e-mail addresses in the current buffer. |
28210 | 129 This function implements `goto-address-highlight-p' |
130 and `goto-address-fontify-p'." | |
30345
d24028985f3d
(goto-address-fontify): Don't bother with buffer-modified and read-only
Dave Love <fx@gnu.org>
parents:
30285
diff
changeset
|
131 ;; Clean up from any previous go. |
d24028985f3d
(goto-address-fontify): Don't bother with buffer-modified and read-only
Dave Love <fx@gnu.org>
parents:
30285
diff
changeset
|
132 (dolist (overlay (overlays-in (point-min) (point-max))) |
d24028985f3d
(goto-address-fontify): Don't bother with buffer-modified and read-only
Dave Love <fx@gnu.org>
parents:
30285
diff
changeset
|
133 (if (overlay-get overlay 'goto-address) |
d24028985f3d
(goto-address-fontify): Don't bother with buffer-modified and read-only
Dave Love <fx@gnu.org>
parents:
30285
diff
changeset
|
134 (delete-overlay overlay))) |
28210 | 135 (save-excursion |
30345
d24028985f3d
(goto-address-fontify): Don't bother with buffer-modified and read-only
Dave Love <fx@gnu.org>
parents:
30285
diff
changeset
|
136 (let ((inhibit-point-motion-hooks t)) |
28210 | 137 (goto-char (point-min)) |
138 (if (< (- (point-max) (point)) goto-address-fontify-maximum-size) | |
139 (progn | |
140 (while (re-search-forward goto-address-url-regexp nil t) | |
141 (let* ((s (match-beginning 0)) | |
142 (e (match-end 0)) | |
143 (this-overlay (make-overlay s e))) | |
144 (and goto-address-fontify-p | |
145 (overlay-put this-overlay 'face goto-address-url-face)) | |
146 (overlay-put this-overlay | |
147 'mouse-face goto-address-url-mouse-face) | |
148 (overlay-put this-overlay | |
29209
67618bef1515
(goto-address-fontify): Add help-echo property.
Dave Love <fx@gnu.org>
parents:
28210
diff
changeset
|
149 'help-echo "mouse-2: follow URL") |
67618bef1515
(goto-address-fontify): Add help-echo property.
Dave Love <fx@gnu.org>
parents:
28210
diff
changeset
|
150 (overlay-put this-overlay |
30345
d24028985f3d
(goto-address-fontify): Don't bother with buffer-modified and read-only
Dave Love <fx@gnu.org>
parents:
30285
diff
changeset
|
151 'keymap goto-address-highlight-keymap) |
d24028985f3d
(goto-address-fontify): Don't bother with buffer-modified and read-only
Dave Love <fx@gnu.org>
parents:
30285
diff
changeset
|
152 (overlay-put this-overlay 'goto-address t))) |
28210 | 153 (goto-char (point-min)) |
154 (while (re-search-forward goto-address-mail-regexp nil t) | |
155 (let* ((s (match-beginning 0)) | |
156 (e (match-end 0)) | |
157 (this-overlay (make-overlay s e))) | |
158 (and goto-address-fontify-p | |
159 (overlay-put this-overlay 'face goto-address-mail-face)) | |
160 (overlay-put this-overlay 'mouse-face | |
161 goto-address-mail-mouse-face) | |
29209
67618bef1515
(goto-address-fontify): Add help-echo property.
Dave Love <fx@gnu.org>
parents:
28210
diff
changeset
|
162 (overlay-put this-overlay |
67618bef1515
(goto-address-fontify): Add help-echo property.
Dave Love <fx@gnu.org>
parents:
28210
diff
changeset
|
163 'help-echo "mouse-2: follow URL") |
28210 | 164 (overlay-put this-overlay |
30345
d24028985f3d
(goto-address-fontify): Don't bother with buffer-modified and read-only
Dave Love <fx@gnu.org>
parents:
30285
diff
changeset
|
165 'keymap goto-address-highlight-keymap) |
d24028985f3d
(goto-address-fontify): Don't bother with buffer-modified and read-only
Dave Love <fx@gnu.org>
parents:
30285
diff
changeset
|
166 (overlay-put this-overlay 'goto-address t)))))))) |
28210 | 167 |
168 ;;; code to find and goto addresses; much of this has been blatantly | |
169 ;;; snarfed from browse-url.el | |
170 | |
171 ;;;###autoload | |
172 (defun goto-address-at-mouse (event) | |
173 "Send to the e-mail address or load the URL clicked with the mouse. | |
174 Send mail to address at position of mouse click. See documentation for | |
175 `goto-address-find-address-at-point'. If no address is found | |
176 there, then load the URL at or before the position of the mouse click." | |
177 (interactive "e") | |
178 (save-excursion | |
179 (let ((posn (event-start event))) | |
180 (set-buffer (window-buffer (posn-window posn))) | |
181 (goto-char (posn-point posn)) | |
182 (let ((address | |
183 (save-excursion (goto-address-find-address-at-point)))) | |
184 (if (string-equal address "") | |
185 (let ((url (browse-url-url-at-point))) | |
186 (if (string-equal url "") | |
187 (error "No e-mail address or URL found") | |
188 (browse-url url))) | |
189 (compose-mail address)))))) | |
190 | |
191 ;;;###autoload | |
192 (defun goto-address-at-point () | |
193 "Send to the e-mail address or load the URL at point. | |
194 Send mail to address at point. See documentation for | |
195 `goto-address-find-address-at-point'. If no address is found | |
196 there, then load the URL at or before point." | |
197 (interactive) | |
198 (save-excursion | |
199 (let ((address (save-excursion (goto-address-find-address-at-point)))) | |
200 (if (string-equal address "") | |
201 (let ((url (browse-url-url-at-point))) | |
202 (if (string-equal url "") | |
203 (error "No e-mail address or URL found") | |
204 (browse-url url))) | |
205 (compose-mail address))))) | |
206 | |
207 (defun goto-address-find-address-at-point () | |
208 "Find e-mail address around or before point. | |
209 Then search backwards to beginning of line for the start of an e-mail | |
210 address. If no e-mail address found, return the empty string." | |
211 (let ((bol (save-excursion (beginning-of-line) (point)))) | |
212 (re-search-backward "[^-_A-z0-9.@]" bol 'lim) | |
213 (if (or (looking-at goto-address-mail-regexp) ; already at start | |
214 (let ((eol (save-excursion (end-of-line) (point)))) | |
215 (and (re-search-forward goto-address-mail-regexp eol 'lim) | |
216 (goto-char (match-beginning 0))))) | |
217 (buffer-substring (match-beginning 0) (match-end 0)) | |
218 "")))m | |
219 | |
220 ;;;###autoload | |
221 (defun goto-address () | |
222 "Sets up goto-address functionality in the current buffer. | |
223 Allows user to use mouse/keyboard command to click to go to a URL | |
224 or to send e-mail. | |
225 By default, goto-address binds to mouse-2 and C-c RET. | |
226 | |
227 Also fontifies the buffer appropriately (see `goto-address-fontify-p' and | |
228 `goto-address-highlight-p' for more information)." | |
229 (interactive) | |
230 (if goto-address-highlight-p | |
231 (goto-address-fontify))) | |
232 | |
233 (provide 'goto-addr) | |
234 | |
235 ;;; goto-addr.el ends here. |