(ange-ftp-allow-child-lookup): Always return nil.
[bpt/emacs.git] / lisp / net / goto-addr.el
CommitLineData
8749abea
GM
1;;; goto-addr.el --- click to browse URL or to send to e-mail address
2
67ac0f7a 3;; Copyright (C) 1995, 2000, 2001 Free Software Foundation, Inc.
8749abea 4
cf6936a4 5;; Author: Eric Ding <ericding@alum.mit.edu>
12c74386 6;; Maintainer: FSF
8749abea
GM
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;;
28223a7e 42;; The mouse click method is bound to [mouse-2] on highlighted URLs or
8749abea
GM
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
8749abea
GM
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)
51107a70
DL
58;; * If the buffer is fontified after goto-address-fontify is run
59;; (say, using font-lock-fontify-buffer), then font-lock faces will
8749abea
GM
60;; override goto-address faces.
61
62;;; Code:
63
f79538c9
DL
64(require 'thingatpt)
65(autoload 'browse-url-url-at-point "browse-url")
8749abea 66
67ac0f7a
GM
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))
76
8749abea
GM
77(defgroup goto-address nil
78 "Click to browse URL or to send to e-mail address."
79 :group 'mouse
80 :group 'hypermedia)
81
82
d8754ce5 83;; I don't expect users to want fontify'ing without highlighting.
8749abea 84(defcustom goto-address-fontify-p t
f79538c9 85 "*Non-nil means URLs and e-mail addresses in buffer are fontified.
8749abea
GM
86But only if `goto-address-highlight-p' is also non-nil."
87 :type 'boolean
88 :group 'goto-address)
89
90(defcustom goto-address-highlight-p t
f79538c9 91 "*Non-nil means URLs and e-mail addresses in buffer are highlighted."
8749abea
GM
92 :type 'boolean
93 :group 'goto-address)
94
95(defcustom goto-address-fontify-maximum-size 30000
28223a7e 96 "*Maximum size of file in which to fontify and/or highlight URLs."
8749abea
GM
97 :type 'integer
98 :group 'goto-address)
99
100(defvar goto-address-mail-regexp
138399e6
SM
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]+"
8749abea
GM
103 "A regular expression probably matching an e-mail address.")
104
f79538c9 105(defvar goto-address-url-regexp thing-at-point-url-regexp
d8754ce5
GM
106 ;; (concat "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|"
107 ;; "telnet\\|wais\\):\\(//[-a-zA-Z0-9_.]+:"
108 ;; "[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*"
109 ;; "[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
8749abea
GM
110 "A regular expression probably matching a URL.")
111
112(defvar goto-address-highlight-keymap
113 (let ((m (make-sparse-keymap)))
67ac0f7a
GM
114 (if (featurep 'xemacs)
115 (define-key m (kbd "<button2>") 'goto-address-at-mouse)
116 (define-key m (kbd "<mouse-2>") 'goto-address-at-mouse))
117 (define-key m (kbd "C-c RET") 'goto-address-at-point)
8749abea
GM
118 m)
119 "keymap to hold goto-addr's mouse key defs under highlighted URLs.")
120
121(defcustom goto-address-url-face 'bold
f79538c9 122 "Face to use for URLs."
8749abea
GM
123 :type 'face
124 :group 'goto-address)
125
126(defcustom goto-address-url-mouse-face 'highlight
f79538c9 127 "Face to use for URLs when the mouse is on them."
8749abea
GM
128 :type 'face
129 :group 'goto-address)
130
131(defcustom goto-address-mail-face 'italic
f79538c9 132 "Face to use for e-mail addresses."
8749abea
GM
133 :type 'face
134 :group 'goto-address)
135
136(defcustom goto-address-mail-mouse-face 'secondary-selection
f79538c9 137 "Face to use for e-mail addresses when the mouse is on them."
8749abea
GM
138 :type 'face
139 :group 'goto-address)
140
141(defun goto-address-fontify ()
28223a7e 142 "Fontify the URLs and e-mail addresses in the current buffer.
8749abea
GM
143This function implements `goto-address-highlight-p'
144and `goto-address-fontify-p'."
51107a70
DL
145 ;; Clean up from any previous go.
146 (dolist (overlay (overlays-in (point-min) (point-max)))
147 (if (overlay-get overlay 'goto-address)
148 (delete-overlay overlay)))
8749abea 149 (save-excursion
51107a70 150 (let ((inhibit-point-motion-hooks t))
8749abea
GM
151 (goto-char (point-min))
152 (if (< (- (point-max) (point)) goto-address-fontify-maximum-size)
153 (progn
154 (while (re-search-forward goto-address-url-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-url-face))
160 (overlay-put this-overlay
161 'mouse-face goto-address-url-mouse-face)
fa59072a
DL
162 (overlay-put this-overlay
163 'help-echo "mouse-2: follow URL")
8749abea 164 (overlay-put this-overlay
51107a70
DL
165 'keymap goto-address-highlight-keymap)
166 (overlay-put this-overlay 'goto-address t)))
8749abea
GM
167 (goto-char (point-min))
168 (while (re-search-forward goto-address-mail-regexp nil t)
169 (let* ((s (match-beginning 0))
170 (e (match-end 0))
171 (this-overlay (make-overlay s e)))
172 (and goto-address-fontify-p
173 (overlay-put this-overlay 'face goto-address-mail-face))
174 (overlay-put this-overlay 'mouse-face
175 goto-address-mail-mouse-face)
fa59072a 176 (overlay-put this-overlay
f79538c9 177 'help-echo "mouse-2: mail this address")
8749abea 178 (overlay-put this-overlay
51107a70
DL
179 'keymap goto-address-highlight-keymap)
180 (overlay-put this-overlay 'goto-address t))))))))
8749abea 181
d8754ce5
GM
182;; code to find and goto addresses; much of this has been blatantly
183;; snarfed from browse-url.el
8749abea
GM
184
185;;;###autoload
186(defun goto-address-at-mouse (event)
187 "Send to the e-mail address or load the URL clicked with the mouse.
188Send mail to address at position of mouse click. See documentation for
189`goto-address-find-address-at-point'. If no address is found
190there, then load the URL at or before the position of the mouse click."
191 (interactive "e")
192 (save-excursion
f79538c9
DL
193 (mouse-set-point event)
194 (goto-address-at-point)))
8749abea
GM
195
196;;;###autoload
197(defun goto-address-at-point ()
198 "Send to the e-mail address or load the URL at point.
199Send mail to address at point. See documentation for
200`goto-address-find-address-at-point'. If no address is found
201there, then load the URL at or before point."
202 (interactive)
203 (save-excursion
204 (let ((address (save-excursion (goto-address-find-address-at-point))))
1c66d9fb
GM
205 (if (and address
206 (save-excursion
207 (goto-char (previous-single-char-property-change
208 (point) 'goto-address nil
209 (line-beginning-position)))
210 (not (looking-at goto-address-url-regexp))))
f79538c9
DL
211 (compose-mail address)
212 (let ((url (browse-url-url-at-point)))
213 (if url
214 (browse-url url)
215 (error "No e-mail address or URL found")))))))
8749abea
GM
216
217(defun goto-address-find-address-at-point ()
218 "Find e-mail address around or before point.
219Then search backwards to beginning of line for the start of an e-mail
f79538c9
DL
220address. If no e-mail address found, return nil."
221 (re-search-backward "[^-_A-z0-9.@]" (line-beginning-position) 'lim)
222 (if (or (looking-at goto-address-mail-regexp) ; already at start
223 (and (re-search-forward goto-address-mail-regexp
224 (line-end-position) 'lim)
225 (goto-char (match-beginning 0))))
226 (match-string-no-properties 0)))
8749abea
GM
227
228;;;###autoload
229(defun goto-address ()
230 "Sets up goto-address functionality in the current buffer.
231Allows user to use mouse/keyboard command to click to go to a URL
232or to send e-mail.
233By default, goto-address binds to mouse-2 and C-c RET.
234
235Also fontifies the buffer appropriately (see `goto-address-fontify-p' and
236`goto-address-highlight-p' for more information)."
237 (interactive)
8749abea
GM
238 (if goto-address-highlight-p
239 (goto-address-fontify)))
240
241(provide 'goto-addr)
242
55535639 243;;; goto-addr.el ends here