Moved here from lisp.
[bpt/emacs.git] / lisp / goto-addr.el
1 ;;; goto-addr.el --- click to browse URL or to send to e-mail address
2
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4
5 ;; Author: Eric Ding <ericding@mit.edu>
6 ;; Maintainer: Eric Ding <ericding@mit.edu>
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 ;;
42 ;; The mouse click method is bound to [mouse-2] on highlighted URL's or
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
78 "*If t, URL's and e-mail addresses in buffer are fontified.
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
84 "*If t, URL's and e-mail addresses in buffer are highlighted."
85 :type 'boolean
86 :group 'goto-address)
87
88 (defcustom goto-address-fontify-maximum-size 30000
89 "*Maximum size of file in which to fontify and/or highlight URL's."
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)
107 m)
108 "keymap to hold goto-addr's mouse key defs under highlighted URLs.")
109
110 (defcustom goto-address-url-face 'bold
111 "*Face to use for URLs."
112 :type 'face
113 :group 'goto-address)
114
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)
119
120 (defcustom goto-address-mail-face 'italic
121 "*Face to use for e-mail addresses."
122 :type 'face
123 :group 'goto-address)
124
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)
129
130 (defun goto-address-fontify ()
131 "Fontify the URL's 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 'local-map goto-address-highlight-keymap)))
151 (goto-char (point-min))
152 (while (re-search-forward goto-address-mail-regexp nil t)
153 (let* ((s (match-beginning 0))
154 (e (match-end 0))
155 (this-overlay (make-overlay s e)))
156 (and goto-address-fontify-p
157 (overlay-put this-overlay 'face goto-address-mail-face))
158 (overlay-put this-overlay 'mouse-face
159 goto-address-mail-mouse-face)
160 (overlay-put this-overlay
161 'local-map goto-address-highlight-keymap)))))
162 (and (buffer-modified-p)
163 (not modified)
164 (set-buffer-modified-p nil)))))
165
166 ;;; code to find and goto addresses; much of this has been blatantly
167 ;;; snarfed from browse-url.el
168
169 ;;;###autoload
170 (defun goto-address-at-mouse (event)
171 "Send to the e-mail address or load the URL clicked with the mouse.
172 Send mail to address at position of mouse click. See documentation for
173 `goto-address-find-address-at-point'. If no address is found
174 there, then load the URL at or before the position of the mouse click."
175 (interactive "e")
176 (save-excursion
177 (let ((posn (event-start event)))
178 (set-buffer (window-buffer (posn-window posn)))
179 (goto-char (posn-point posn))
180 (let ((address
181 (save-excursion (goto-address-find-address-at-point))))
182 (if (string-equal address "")
183 (let ((url (browse-url-url-at-point)))
184 (if (string-equal url "")
185 (error "No e-mail address or URL found")
186 (browse-url url)))
187 (compose-mail address))))))
188
189 ;;;###autoload
190 (defun goto-address-at-point ()
191 "Send to the e-mail address or load the URL at point.
192 Send mail to address at point. See documentation for
193 `goto-address-find-address-at-point'. If no address is found
194 there, then load the URL at or before point."
195 (interactive)
196 (save-excursion
197 (let ((address (save-excursion (goto-address-find-address-at-point))))
198 (if (string-equal address "")
199 (let ((url (browse-url-url-at-point)))
200 (if (string-equal url "")
201 (error "No e-mail address or URL found")
202 (browse-url url)))
203 (compose-mail address)))))
204
205 (defun goto-address-find-address-at-point ()
206 "Find e-mail address around or before point.
207 Then search backwards to beginning of line for the start of an e-mail
208 address. If no e-mail address found, return the empty string."
209 (let ((bol (save-excursion (beginning-of-line) (point))))
210 (re-search-backward "[^-_A-z0-9.@]" bol 'lim)
211 (if (or (looking-at goto-address-mail-regexp) ; already at start
212 (let ((eol (save-excursion (end-of-line) (point))))
213 (and (re-search-forward goto-address-mail-regexp eol 'lim)
214 (goto-char (match-beginning 0)))))
215 (buffer-substring (match-beginning 0) (match-end 0))
216 "")))m
217
218 ;;;###autoload
219 (defun goto-address ()
220 "Sets up goto-address functionality in the current buffer.
221 Allows user to use mouse/keyboard command to click to go to a URL
222 or to send e-mail.
223 By default, goto-address binds to mouse-2 and C-c RET.
224
225 Also fontifies the buffer appropriately (see `goto-address-fontify-p' and
226 `goto-address-highlight-p' for more information)."
227 (interactive)
228 (local-set-key "\C-c\r" 'goto-address-at-point)
229 (if goto-address-highlight-p
230 (goto-address-fontify)))
231
232 (provide 'goto-addr)
233
234 ;;; goto-addr.el ends here.