Mercurial > emacs
annotate lisp/net/goto-addr.el @ 35928:83885ae4db3a
(EMACS): Use $(THISDIR) to make emacs.exe path
absolute.
author | Andrew Innes <andrewi@gnu.org> |
---|---|
date | Tue, 06 Feb 2001 15:51:02 +0000 |
parents | 42ce5997aa4a |
children | 3ab2c865a8e5 |
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 | |
30891
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
64 (require 'thingatpt) |
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
65 (autoload 'browse-url-url-at-point "browse-url") |
28210 | 66 |
67 (defgroup goto-address nil | |
68 "Click to browse URL or to send to e-mail address." | |
69 :group 'mouse | |
70 :group 'hypermedia) | |
71 | |
72 | |
73 ;;; I don't expect users to want fontify'ing without highlighting. | |
74 (defcustom goto-address-fontify-p t | |
30891
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
75 "*Non-nil means URLs and e-mail addresses in buffer are fontified. |
28210 | 76 But only if `goto-address-highlight-p' is also non-nil." |
77 :type 'boolean | |
78 :group 'goto-address) | |
79 | |
80 (defcustom goto-address-highlight-p t | |
30891
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
81 "*Non-nil means URLs and e-mail addresses in buffer are highlighted." |
28210 | 82 :type 'boolean |
83 :group 'goto-address) | |
84 | |
85 (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
|
86 "*Maximum size of file in which to fontify and/or highlight URLs." |
28210 | 87 :type 'integer |
88 :group 'goto-address) | |
89 | |
90 (defvar goto-address-mail-regexp | |
91 "[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" | |
92 "A regular expression probably matching an e-mail address.") | |
93 | |
30891
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
94 (defvar goto-address-url-regexp thing-at-point-url-regexp |
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
95 ;;; (concat "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|" |
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
96 ;;; "telnet\\|wais\\):\\(//[-a-zA-Z0-9_.]+:" |
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
97 ;;; "[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*" |
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
98 ;;; "[-a-zA-Z0-9_=#$@~`%&*+|\\/]") |
28210 | 99 "A regular expression probably matching a URL.") |
100 | |
101 (defvar goto-address-highlight-keymap | |
102 (let ((m (make-sparse-keymap))) | |
103 (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
|
104 (define-key m "\C-c\r" 'goto-address-at-point) |
28210 | 105 m) |
106 "keymap to hold goto-addr's mouse key defs under highlighted URLs.") | |
107 | |
108 (defcustom goto-address-url-face 'bold | |
30891
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
109 "Face to use for URLs." |
28210 | 110 :type 'face |
111 :group 'goto-address) | |
112 | |
113 (defcustom goto-address-url-mouse-face 'highlight | |
30891
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
114 "Face to use for URLs when the mouse is on them." |
28210 | 115 :type 'face |
116 :group 'goto-address) | |
117 | |
118 (defcustom goto-address-mail-face 'italic | |
30891
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
119 "Face to use for e-mail addresses." |
28210 | 120 :type 'face |
121 :group 'goto-address) | |
122 | |
123 (defcustom goto-address-mail-mouse-face 'secondary-selection | |
30891
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
124 "Face to use for e-mail addresses when the mouse is on them." |
28210 | 125 :type 'face |
126 :group 'goto-address) | |
127 | |
128 (defun goto-address-fontify () | |
29664
3e03d72fadea
(goto-address-fontify): Use keymap property, not local-map.
Dave Love <fx@gnu.org>
parents:
29653
diff
changeset
|
129 "Fontify the URLs and e-mail addresses in the current buffer. |
28210 | 130 This function implements `goto-address-highlight-p' |
131 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
|
132 ;; 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
|
133 (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
|
134 (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
|
135 (delete-overlay overlay))) |
28210 | 136 (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
|
137 (let ((inhibit-point-motion-hooks t)) |
28210 | 138 (goto-char (point-min)) |
139 (if (< (- (point-max) (point)) goto-address-fontify-maximum-size) | |
140 (progn | |
141 (while (re-search-forward goto-address-url-regexp nil t) | |
142 (let* ((s (match-beginning 0)) | |
143 (e (match-end 0)) | |
144 (this-overlay (make-overlay s e))) | |
145 (and goto-address-fontify-p | |
146 (overlay-put this-overlay 'face goto-address-url-face)) | |
147 (overlay-put this-overlay | |
148 'mouse-face goto-address-url-mouse-face) | |
149 (overlay-put this-overlay | |
29209
67618bef1515
(goto-address-fontify): Add help-echo property.
Dave Love <fx@gnu.org>
parents:
28210
diff
changeset
|
150 'help-echo "mouse-2: follow URL") |
67618bef1515
(goto-address-fontify): Add help-echo property.
Dave Love <fx@gnu.org>
parents:
28210
diff
changeset
|
151 (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
|
152 '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
|
153 (overlay-put this-overlay 'goto-address t))) |
28210 | 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) | |
29209
67618bef1515
(goto-address-fontify): Add help-echo property.
Dave Love <fx@gnu.org>
parents:
28210
diff
changeset
|
163 (overlay-put this-overlay |
30891
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
164 'help-echo "mouse-2: mail this address") |
28210 | 165 (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
|
166 '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
|
167 (overlay-put this-overlay 'goto-address t)))))))) |
28210 | 168 |
169 ;;; code to find and goto addresses; much of this has been blatantly | |
170 ;;; snarfed from browse-url.el | |
171 | |
172 ;;;###autoload | |
173 (defun goto-address-at-mouse (event) | |
174 "Send to the e-mail address or load the URL clicked with the mouse. | |
175 Send mail to address at position of mouse click. See documentation for | |
176 `goto-address-find-address-at-point'. If no address is found | |
177 there, then load the URL at or before the position of the mouse click." | |
178 (interactive "e") | |
179 (save-excursion | |
30891
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
180 (mouse-set-point event) |
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
181 (goto-address-at-point))) |
28210 | 182 |
183 ;;;###autoload | |
184 (defun goto-address-at-point () | |
185 "Send to the e-mail address or load the URL at point. | |
186 Send mail to address at point. See documentation for | |
187 `goto-address-find-address-at-point'. If no address is found | |
188 there, then load the URL at or before point." | |
189 (interactive) | |
190 (save-excursion | |
191 (let ((address (save-excursion (goto-address-find-address-at-point)))) | |
30891
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
192 (if address |
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
193 (compose-mail address) |
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
194 (let ((url (browse-url-url-at-point))) |
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
195 (if url |
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
196 (browse-url url) |
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
197 (error "No e-mail address or URL found"))))))) |
28210 | 198 |
199 (defun goto-address-find-address-at-point () | |
200 "Find e-mail address around or before point. | |
201 Then search backwards to beginning of line for the start of an e-mail | |
30891
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
202 address. If no e-mail address found, return nil." |
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
203 (re-search-backward "[^-_A-z0-9.@]" (line-beginning-position) 'lim) |
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
204 (if (or (looking-at goto-address-mail-regexp) ; already at start |
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
205 (and (re-search-forward goto-address-mail-regexp |
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
206 (line-end-position) 'lim) |
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
207 (goto-char (match-beginning 0)))) |
42ce5997aa4a
Don't require browse-url. Require thingatpt.
Dave Love <fx@gnu.org>
parents:
30345
diff
changeset
|
208 (match-string-no-properties 0))) |
28210 | 209 |
210 ;;;###autoload | |
211 (defun goto-address () | |
212 "Sets up goto-address functionality in the current buffer. | |
213 Allows user to use mouse/keyboard command to click to go to a URL | |
214 or to send e-mail. | |
215 By default, goto-address binds to mouse-2 and C-c RET. | |
216 | |
217 Also fontifies the buffer appropriately (see `goto-address-fontify-p' and | |
218 `goto-address-highlight-p' for more information)." | |
219 (interactive) | |
220 (if goto-address-highlight-p | |
221 (goto-address-fontify))) | |
222 | |
223 (provide 'goto-addr) | |
224 | |
225 ;;; goto-addr.el ends here. |