]> code.delx.au - gnu-emacs/blob - lisp/net/goto-addr.el
Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-77
[gnu-emacs] / lisp / net / goto-addr.el
1 ;;; goto-addr.el --- click to browse URL or to send to e-mail address
2
3 ;; Copyright (C) 1995, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
5
6 ;; Author: Eric Ding <ericding@alum.mit.edu>
7 ;; Maintainer: FSF
8 ;; Created: 15 Aug 1995
9 ;; Keywords: mh-e, www, mouse, mail
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Commentary:
29
30 ;; This package allows you to click or hit a key sequence while on a
31 ;; URL or e-mail address, and either load the URL into a browser of
32 ;; your choice using the browse-url package, or if it's an e-mail
33 ;; address, to send an e-mail to that address. By default, we bind to
34 ;; the [mouse-2] and the [C-c return] key sequences.
35
36 ;; INSTALLATION
37 ;;
38 ;; To use goto-address in a particular mode (for example, while
39 ;; reading mail in mh-e), add something like this in your .emacs file:
40 ;;
41 ;; (add-hook 'mh-show-mode-hook 'goto-address)
42 ;;
43 ;; The mouse click method is bound to [mouse-2] on highlighted URLs or
44 ;; e-mail addresses only; it functions normally everywhere else. To bind
45 ;; another mouse click to the function, add the following to your .emacs
46 ;; (for example):
47 ;;
48 ;; (setq goto-address-highlight-keymap
49 ;; (let ((m (make-sparse-keymap)))
50 ;; (define-key m [S-mouse-2] 'goto-address-at-point)
51 ;; m))
52 ;;
53
54 ;; Known bugs/features:
55 ;; * goto-address-mail-regexp only catches foo@bar.org style addressing,
56 ;; not stuff like X.400 addresses, etc.
57 ;; * regexp also catches Message-Id line, since it is in the format of
58 ;; an Internet e-mail address (like Compuserve addresses)
59 ;; * If the buffer is fontified after goto-address-fontify is run
60 ;; (say, using font-lock-fontify-buffer), then font-lock faces will
61 ;; override goto-address faces.
62
63 ;;; Code:
64
65 (require 'thingatpt)
66 (autoload 'browse-url-url-at-point "browse-url")
67
68 ;; XEmacs needs the following definitions.
69 (unless (fboundp 'overlays-in)
70 (require 'overlay))
71 (unless (fboundp 'line-beginning-position)
72 (defalias 'line-beginning-position 'point-at-bol))
73 (unless (fboundp 'line-end-position)
74 (defalias 'line-end-position 'point-at-eol))
75 (unless (fboundp 'match-string-no-properties)
76 (defalias 'match-string-no-properties 'match-string))
77
78 (defgroup goto-address nil
79 "Click to browse URL or to send to e-mail address."
80 :group 'mouse
81 :group 'hypermedia)
82
83
84 ;; I don't expect users to want fontify'ing without highlighting.
85 (defcustom goto-address-fontify-p t
86 "*Non-nil means URLs and e-mail addresses in buffer are fontified.
87 But only if `goto-address-highlight-p' is also non-nil."
88 :type 'boolean
89 :group 'goto-address)
90
91 (defcustom goto-address-highlight-p t
92 "*Non-nil means URLs and e-mail addresses in buffer are highlighted."
93 :type 'boolean
94 :group 'goto-address)
95
96 (defcustom goto-address-fontify-maximum-size 30000
97 "*Maximum size of file in which to fontify and/or highlight URLs.
98 A value of t means there is no limit--fontify regardless of the size."
99 :type '(choice (integer :tag "Maximum size") (const :tag "No limit" t))
100 :group 'goto-address)
101
102 (defvar goto-address-mail-regexp
103 ;; Actually pretty much any char could appear in the username part. -stef
104 "[-a-zA-Z0-9=._+]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+"
105 "A regular expression probably matching an e-mail address.")
106
107 (defvar goto-address-url-regexp
108 (concat "\\<\\("
109 (mapconcat 'identity
110 (delete "mailto:" (copy-sequence thing-at-point-uri-schemes))
111 "\\|")
112 "\\)"
113 thing-at-point-url-path-regexp)
114 ;; (concat "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|"
115 ;; "telnet\\|wais\\):\\(//[-a-zA-Z0-9_.]+:"
116 ;; "[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*"
117 ;; "[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
118 "A regular expression probably matching a URL.")
119
120 (defvar goto-address-highlight-keymap
121 (let ((m (make-sparse-keymap)))
122 (define-key m (if (featurep 'xemacs) (kbd "<button2>") (kbd "<mouse-2>"))
123 'goto-address-at-point)
124 (define-key m (kbd "C-c RET") 'goto-address-at-point)
125 m)
126 "keymap to hold goto-addr's mouse key defs under highlighted URLs.")
127
128 (defcustom goto-address-url-face 'bold
129 "Face to use for URLs."
130 :type 'face
131 :group 'goto-address)
132
133 (defcustom goto-address-url-mouse-face 'highlight
134 "Face to use for URLs when the mouse is on them."
135 :type 'face
136 :group 'goto-address)
137
138 (defcustom goto-address-mail-face 'italic
139 "Face to use for e-mail addresses."
140 :type 'face
141 :group 'goto-address)
142
143 (defcustom goto-address-mail-mouse-face 'secondary-selection
144 "Face to use for e-mail addresses when the mouse is on them."
145 :type 'face
146 :group 'goto-address)
147
148 (defun goto-address-fontify ()
149 "Fontify the URLs and e-mail addresses in the current buffer.
150 This function implements `goto-address-highlight-p'
151 and `goto-address-fontify-p'."
152 ;; Clean up from any previous go.
153 (dolist (overlay (overlays-in (point-min) (point-max)))
154 (if (overlay-get overlay 'goto-address)
155 (delete-overlay overlay)))
156 (save-excursion
157 (let ((inhibit-point-motion-hooks t))
158 (goto-char (point-min))
159 (if (or (eq t goto-address-fontify-maximum-size)
160 (< (- (point-max) (point)) goto-address-fontify-maximum-size))
161 (progn
162 (while (re-search-forward goto-address-url-regexp nil t)
163 (let* ((s (match-beginning 0))
164 (e (match-end 0))
165 (this-overlay (make-overlay s e)))
166 (and goto-address-fontify-p
167 (overlay-put this-overlay 'face goto-address-url-face))
168 (overlay-put this-overlay 'evaporate t)
169 (overlay-put this-overlay
170 'mouse-face goto-address-url-mouse-face)
171 (overlay-put this-overlay
172 'help-echo "mouse-2, C-c RET: follow URL")
173 (overlay-put this-overlay
174 'keymap goto-address-highlight-keymap)
175 (overlay-put this-overlay 'goto-address t)))
176 (goto-char (point-min))
177 (while (re-search-forward goto-address-mail-regexp nil t)
178 (let* ((s (match-beginning 0))
179 (e (match-end 0))
180 (this-overlay (make-overlay s e)))
181 (and goto-address-fontify-p
182 (overlay-put this-overlay 'face goto-address-mail-face))
183 (overlay-put this-overlay 'evaporate t)
184 (overlay-put this-overlay 'mouse-face
185 goto-address-mail-mouse-face)
186 (overlay-put this-overlay
187 'help-echo "mouse-2, C-c RET: mail this address")
188 (overlay-put this-overlay
189 'keymap goto-address-highlight-keymap)
190 (overlay-put this-overlay 'goto-address t))))))))
191
192 ;; code to find and goto addresses; much of this has been blatantly
193 ;; snarfed from browse-url.el
194
195 ;;;###autoload
196 (define-obsolete-function-alias
197 'goto-address-at-mouse 'goto-address-at-point "22.1")
198
199 ;;;###autoload
200 (defun goto-address-at-point (&optional event)
201 "Send to the e-mail address or load the URL at point.
202 Send mail to address at point. See documentation for
203 `goto-address-find-address-at-point'. If no address is found
204 there, then load the URL at or before point."
205 (interactive (list last-input-event))
206 (save-excursion
207 (if event (mouse-set-point event))
208 (let ((address (save-excursion (goto-address-find-address-at-point))))
209 (if (and address
210 (save-excursion
211 (goto-char (previous-single-char-property-change
212 (point) 'goto-address nil
213 (line-beginning-position)))
214 (not (looking-at goto-address-url-regexp))))
215 (compose-mail address)
216 (let ((url (browse-url-url-at-point)))
217 (if url
218 (browse-url url)
219 (error "No e-mail address or URL found")))))))
220
221 (defun goto-address-find-address-at-point ()
222 "Find e-mail address around or before point.
223 Then search backwards to beginning of line for the start of an e-mail
224 address. If no e-mail address found, return nil."
225 (re-search-backward "[^-_A-z0-9.@]" (line-beginning-position) 'lim)
226 (if (or (looking-at goto-address-mail-regexp) ; already at start
227 (and (re-search-forward goto-address-mail-regexp
228 (line-end-position) 'lim)
229 (goto-char (match-beginning 0))))
230 (match-string-no-properties 0)))
231
232 ;;;###autoload
233 (defun goto-address ()
234 "Sets up goto-address functionality in the current buffer.
235 Allows user to use mouse/keyboard command to click to go to a URL
236 or to send e-mail.
237 By default, goto-address binds to mouse-2 and C-c RET.
238
239 Also fontifies the buffer appropriately (see `goto-address-fontify-p' and
240 `goto-address-highlight-p' for more information)."
241 (interactive)
242 (if goto-address-highlight-p
243 (goto-address-fontify)))
244
245 (provide 'goto-addr)
246
247 ;; arch-tag: ca47c505-5661-425d-a471-62bc6e75cf0a
248 ;;; goto-addr.el ends here