(insert-directory): Remove --dired switch when
[emacs.git] / lisp / net / goto-addr.el
blob42c4bca8bf8ebc37ef15f9b2e76e35efb186c82d
1 ;;; goto-addr.el --- click to browse URL or to send to e-mail address
3 ;; Copyright (C) 1995, 2000, 2001 Free Software Foundation, Inc.
5 ;; Author: Eric Ding <ericding@alum.mit.edu>
6 ;; Maintainer: FSF
7 ;; Created: 15 Aug 1995
8 ;; Keywords: mh-e, www, mouse, mail
10 ;; This file is part of GNU Emacs.
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.
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.
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.
27 ;;; Commentary:
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.
35 ;; INSTALLATION
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:
40 ;; (add-hook 'mh-show-mode-hook 'goto-address)
42 ;; The mouse click method is bound to [mouse-2] on highlighted URLs 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):
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))
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)
58 ;; * If the buffer is fontified after goto-address-fontify is run
59 ;; (say, using font-lock-fontify-buffer), then font-lock faces will
60 ;; override goto-address faces.
62 ;;; Code:
64 (require 'thingatpt)
65 (autoload 'browse-url-url-at-point "browse-url")
67 ;; XEmacs needs the following definitions.
68 (unless (fboundp 'overlays-in)
69 (require 'overlay))
70 (unless (fboundp 'line-beginning-position)
71 (defalias 'line-beginning-position 'point-at-bol))
72 (unless (fboundp 'line-end-position)
73 (defalias 'line-end-position 'point-at-eol))
74 (unless (fboundp 'match-string-no-properties)
75 (defalias 'match-string-no-properties 'match-string))
77 (defgroup goto-address nil
78 "Click to browse URL or to send to e-mail address."
79 :group 'mouse
80 :group 'hypermedia)
83 ;; I don't expect users to want fontify'ing without highlighting.
84 (defcustom goto-address-fontify-p t
85 "*Non-nil means URLs and e-mail addresses in buffer are fontified.
86 But only if `goto-address-highlight-p' is also non-nil."
87 :type 'boolean
88 :group 'goto-address)
90 (defcustom goto-address-highlight-p t
91 "*Non-nil means URLs and e-mail addresses in buffer are highlighted."
92 :type 'boolean
93 :group 'goto-address)
95 (defcustom goto-address-fontify-maximum-size 30000
96 "*Maximum size of file in which to fontify and/or highlight URLs."
97 :type 'integer
98 :group 'goto-address)
100 (defvar goto-address-mail-regexp
101 ;; Actually pretty much any char could appear in the username part. -stef
102 "[-a-zA-Z0-9._+]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+"
103 "A regular expression probably matching an e-mail address.")
105 (defvar goto-address-url-regexp
106 (concat "\\<\\("
107 (mapconcat 'identity
108 (delete "mailto:" (copy-sequence thing-at-point-uri-schemes))
109 "\\|")
110 "\\)"
111 thing-at-point-url-path-regexp)
112 ;; (concat "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|"
113 ;; "telnet\\|wais\\):\\(//[-a-zA-Z0-9_.]+:"
114 ;; "[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*"
115 ;; "[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
116 "A regular expression probably matching a URL.")
118 (defvar goto-address-highlight-keymap
119 (let ((m (make-sparse-keymap)))
120 (if (featurep 'xemacs)
121 (define-key m (kbd "<button2>") 'goto-address-at-mouse)
122 (define-key m (kbd "<mouse-2>") 'goto-address-at-mouse))
123 (define-key m (kbd "C-c RET") 'goto-address-at-point)
125 "keymap to hold goto-addr's mouse key defs under highlighted URLs.")
127 (defcustom goto-address-url-face 'bold
128 "Face to use for URLs."
129 :type 'face
130 :group 'goto-address)
132 (defcustom goto-address-url-mouse-face 'highlight
133 "Face to use for URLs when the mouse is on them."
134 :type 'face
135 :group 'goto-address)
137 (defcustom goto-address-mail-face 'italic
138 "Face to use for e-mail addresses."
139 :type 'face
140 :group 'goto-address)
142 (defcustom goto-address-mail-mouse-face 'secondary-selection
143 "Face to use for e-mail addresses when the mouse is on them."
144 :type 'face
145 :group 'goto-address)
147 (defun goto-address-fontify ()
148 "Fontify the URLs and e-mail addresses in the current buffer.
149 This function implements `goto-address-highlight-p'
150 and `goto-address-fontify-p'."
151 ;; Clean up from any previous go.
152 (dolist (overlay (overlays-in (point-min) (point-max)))
153 (if (overlay-get overlay 'goto-address)
154 (delete-overlay overlay)))
155 (save-excursion
156 (let ((inhibit-point-motion-hooks t))
157 (goto-char (point-min))
158 (if (< (- (point-max) (point)) goto-address-fontify-maximum-size)
159 (progn
160 (while (re-search-forward goto-address-url-regexp nil t)
161 (let* ((s (match-beginning 0))
162 (e (match-end 0))
163 (this-overlay (make-overlay s e)))
164 (and goto-address-fontify-p
165 (overlay-put this-overlay 'face goto-address-url-face))
166 (overlay-put this-overlay
167 'mouse-face goto-address-url-mouse-face)
168 (overlay-put this-overlay
169 'help-echo "mouse-2: follow URL")
170 (overlay-put this-overlay
171 'keymap goto-address-highlight-keymap)
172 (overlay-put this-overlay 'goto-address t)))
173 (goto-char (point-min))
174 (while (re-search-forward goto-address-mail-regexp nil t)
175 (let* ((s (match-beginning 0))
176 (e (match-end 0))
177 (this-overlay (make-overlay s e)))
178 (and goto-address-fontify-p
179 (overlay-put this-overlay 'face goto-address-mail-face))
180 (overlay-put this-overlay 'mouse-face
181 goto-address-mail-mouse-face)
182 (overlay-put this-overlay
183 'help-echo "mouse-2: mail this address")
184 (overlay-put this-overlay
185 'keymap goto-address-highlight-keymap)
186 (overlay-put this-overlay 'goto-address t))))))))
188 ;; code to find and goto addresses; much of this has been blatantly
189 ;; snarfed from browse-url.el
191 ;;;###autoload
192 (defun goto-address-at-mouse (event)
193 "Send to the e-mail address or load the URL clicked with the mouse.
194 Send mail to address at position of mouse click. See documentation for
195 `goto-address-find-address-at-point'. If no address is found
196 there, then load the URL at or before the position of the mouse click."
197 (interactive "e")
198 (save-excursion
199 (mouse-set-point event)
200 (goto-address-at-point)))
202 ;;;###autoload
203 (defun goto-address-at-point ()
204 "Send to the e-mail address or load the URL at point.
205 Send mail to address at point. See documentation for
206 `goto-address-find-address-at-point'. If no address is found
207 there, then load the URL at or before point."
208 (interactive)
209 (save-excursion
210 (let ((address (save-excursion (goto-address-find-address-at-point))))
211 (if (and address
212 (save-excursion
213 (goto-char (previous-single-char-property-change
214 (point) 'goto-address nil
215 (line-beginning-position)))
216 (not (looking-at goto-address-url-regexp))))
217 (compose-mail address)
218 (let ((url (browse-url-url-at-point)))
219 (if url
220 (browse-url url)
221 (error "No e-mail address or URL found")))))))
223 (defun goto-address-find-address-at-point ()
224 "Find e-mail address around or before point.
225 Then search backwards to beginning of line for the start of an e-mail
226 address. If no e-mail address found, return nil."
227 (re-search-backward "[^-_A-z0-9.@]" (line-beginning-position) 'lim)
228 (if (or (looking-at goto-address-mail-regexp) ; already at start
229 (and (re-search-forward goto-address-mail-regexp
230 (line-end-position) 'lim)
231 (goto-char (match-beginning 0))))
232 (match-string-no-properties 0)))
234 ;;;###autoload
235 (defun goto-address ()
236 "Sets up goto-address functionality in the current buffer.
237 Allows user to use mouse/keyboard command to click to go to a URL
238 or to send e-mail.
239 By default, goto-address binds to mouse-2 and C-c RET.
241 Also fontifies the buffer appropriately (see `goto-address-fontify-p' and
242 `goto-address-highlight-p' for more information)."
243 (interactive)
244 (if goto-address-highlight-p
245 (goto-address-fontify)))
247 (provide 'goto-addr)
249 ;;; goto-addr.el ends here