Change maintainer address.
[emacs.git] / lisp / net / goto-addr.el
blob095e1c597c7950841972e733670652518ec07cf4
1 ;;; goto-addr.el --- click to browse URL or to send to e-mail address
3 ;; Copyright (C) 1995, 2000 Free Software Foundation, Inc.
5 ;; Author: Eric Ding <ericding@alum.mit.edu>
6 ;; Created: 15 Aug 1995
7 ;; Keywords: mh-e, www, mouse, mail
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
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.
34 ;; INSTALLATION
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:
39 ;; (add-hook 'mh-show-mode-hook 'goto-address)
41 ;; The mouse click method is bound to [mouse-2] on highlighted URLs or
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):
46 ;; (setq goto-address-highlight-keymap
47 ;; (let ((m (make-sparse-keymap)))
48 ;; (define-key m [S-mouse-2] 'goto-address-at-mouse)
49 ;; m))
52 ;; BUG REPORTS
54 ;; Please send bug reports to me at ericding@mit.edu.
56 ;; Known bugs/features:
57 ;; * goto-address-mail-regexp only catches foo@bar.org style addressing,
58 ;; not stuff like X.400 addresses, etc.
59 ;; * regexp also catches Message-Id line, since it is in the format of
60 ;; an Internet e-mail address (like Compuserve addresses)
61 ;; * If show buffer is fontified after goto-address-fontify is run
62 ;; (say, using font-lock-fontify-buffer), then font-lock face will
63 ;; override goto-address faces.
65 ;;; Code:
67 (require 'browse-url)
69 (defgroup goto-address nil
70 "Click to browse URL or to send to e-mail address."
71 :group 'mouse
72 :group 'hypermedia)
75 ;;; I don't expect users to want fontify'ing without highlighting.
76 (defcustom goto-address-fontify-p t
77 "*If t, URLs and e-mail addresses in buffer are fontified.
78 But only if `goto-address-highlight-p' is also non-nil."
79 :type 'boolean
80 :group 'goto-address)
82 (defcustom goto-address-highlight-p t
83 "*If t, URLs and e-mail addresses in buffer are highlighted."
84 :type 'boolean
85 :group 'goto-address)
87 (defcustom goto-address-fontify-maximum-size 30000
88 "*Maximum size of file in which to fontify and/or highlight URLs."
89 :type 'integer
90 :group 'goto-address)
92 (defvar goto-address-mail-regexp
93 "[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+"
94 "A regular expression probably matching an e-mail address.")
96 (defvar goto-address-url-regexp
97 (concat "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|"
98 "telnet\\|wais\\):\\(//[-a-zA-Z0-9_.]+:"
99 "[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*"
100 "[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
101 "A regular expression probably matching a URL.")
103 (defvar goto-address-highlight-keymap
104 (let ((m (make-sparse-keymap)))
105 (define-key m [mouse-2] 'goto-address-at-mouse)
106 (define-key m "\C-c\r" 'goto-address-at-point)
108 "keymap to hold goto-addr's mouse key defs under highlighted URLs.")
110 (defcustom goto-address-url-face 'bold
111 "*Face to use for URLs."
112 :type 'face
113 :group 'goto-address)
115 (defcustom goto-address-url-mouse-face 'highlight
116 "*Face to use for URLs when the mouse is on them."
117 :type 'face
118 :group 'goto-address)
120 (defcustom goto-address-mail-face 'italic
121 "*Face to use for e-mail addresses."
122 :type 'face
123 :group 'goto-address)
125 (defcustom goto-address-mail-mouse-face 'secondary-selection
126 "*Face to use for e-mail addresses when the mouse is on them."
127 :type 'face
128 :group 'goto-address)
130 (defun goto-address-fontify ()
131 "Fontify the URLs and e-mail addresses in the current buffer.
132 This function implements `goto-address-highlight-p'
133 and `goto-address-fontify-p'."
134 (save-excursion
135 (let ((inhibit-read-only t)
136 (inhibit-point-motion-hooks t)
137 (modified (buffer-modified-p)))
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
150 'help-echo "mouse-2: follow URL")
151 (overlay-put this-overlay
152 'keymap goto-address-highlight-keymap)))
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)
162 (overlay-put this-overlay
163 'help-echo "mouse-2: follow URL")
164 (overlay-put this-overlay
165 'keymap goto-address-highlight-keymap)))))
166 (and (buffer-modified-p)
167 (not modified)
168 (set-buffer-modified-p nil)))))
170 ;;; code to find and goto addresses; much of this has been blatantly
171 ;;; snarfed from browse-url.el
173 ;;;###autoload
174 (defun goto-address-at-mouse (event)
175 "Send to the e-mail address or load the URL clicked with the mouse.
176 Send mail to address at position of mouse click. See documentation for
177 `goto-address-find-address-at-point'. If no address is found
178 there, then load the URL at or before the position of the mouse click."
179 (interactive "e")
180 (save-excursion
181 (let ((posn (event-start event)))
182 (set-buffer (window-buffer (posn-window posn)))
183 (goto-char (posn-point posn))
184 (let ((address
185 (save-excursion (goto-address-find-address-at-point))))
186 (if (string-equal address "")
187 (let ((url (browse-url-url-at-point)))
188 (if (string-equal url "")
189 (error "No e-mail address or URL found")
190 (browse-url url)))
191 (compose-mail address))))))
193 ;;;###autoload
194 (defun goto-address-at-point ()
195 "Send to the e-mail address or load the URL at point.
196 Send mail to address at point. See documentation for
197 `goto-address-find-address-at-point'. If no address is found
198 there, then load the URL at or before point."
199 (interactive)
200 (save-excursion
201 (let ((address (save-excursion (goto-address-find-address-at-point))))
202 (if (string-equal address "")
203 (let ((url (browse-url-url-at-point)))
204 (if (string-equal url "")
205 (error "No e-mail address or URL found")
206 (browse-url url)))
207 (compose-mail address)))))
209 (defun goto-address-find-address-at-point ()
210 "Find e-mail address around or before point.
211 Then search backwards to beginning of line for the start of an e-mail
212 address. If no e-mail address found, return the empty string."
213 (let ((bol (save-excursion (beginning-of-line) (point))))
214 (re-search-backward "[^-_A-z0-9.@]" bol 'lim)
215 (if (or (looking-at goto-address-mail-regexp) ; already at start
216 (let ((eol (save-excursion (end-of-line) (point))))
217 (and (re-search-forward goto-address-mail-regexp eol 'lim)
218 (goto-char (match-beginning 0)))))
219 (buffer-substring (match-beginning 0) (match-end 0))
220 "")))m
222 ;;;###autoload
223 (defun goto-address ()
224 "Sets up goto-address functionality in the current buffer.
225 Allows user to use mouse/keyboard command to click to go to a URL
226 or to send e-mail.
227 By default, goto-address binds to mouse-2 and C-c RET.
229 Also fontifies the buffer appropriately (see `goto-address-fontify-p' and
230 `goto-address-highlight-p' for more information)."
231 (interactive)
232 (if goto-address-highlight-p
233 (goto-address-fontify)))
235 (provide 'goto-addr)
237 ;;; goto-addr.el ends here.