(struct window): New member height_fixed_p.
[bpt/emacs.git] / lisp / goto-addr.el
CommitLineData
a69315a1 1;;; goto-addr.el --- click to browse URL or to send to e-mail address
b578f267 2
a69315a1
RS
3;; Copyright (C) 1995 Free Software Foundation, Inc.
4
e137161b 5;; Author: Eric Ding <ericding@mit.edu>
a69315a1
RS
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
b578f267
EN
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.
a69315a1
RS
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
7caa5416 33;; the [mouse-2] and the [C-c return] key sequences.
a69315a1
RS
34
35;; INSTALLATION
36;;
e137161b
RS
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:
a69315a1
RS
39;;
40;; (add-hook 'mh-show-mode-hook 'goto-address)
41;;
42;; By default, goto-address now sends using `mail' instead of `mh-send'.
43;; To use mh-e to send mail, add the following to your .emacs file:
44;;
eba8dcd0 45;; (setq goto-address-mail-method 'goto-address-send-using-mh-e)
a69315a1 46;;
7caa5416
KH
47;; The mouse click method is bound to [mouse-2] on highlighted URL's or
48;; e-mail addresses only; it functions normally everywhere else. To bind
49;; another mouse click to the function, add the following to your .emacs
50;; (for example):
a69315a1 51;;
7caa5416
KH
52;; (setq goto-address-highlight-keymap
53;; (let ((m (make-sparse-keymap)))
54;; (define-key m [S-mouse-2] 'goto-address-at-mouse)
55;; m))
a69315a1 56;;
a69315a1
RS
57
58;; BUG REPORTS
59;;
60;; Please send bug reports to me at ericding@mit.edu.
61
62;; Known bugs/features:
63;; * goto-address-mail-regexp only catches foo@bar.org style addressing,
64;; not stuff like X.400 addresses, etc.
65;; * regexp also catches Message-Id line, since it is in the format of
66;; an Internet e-mail address (like Compuserve addresses)
67;; * If show buffer is fontified after goto-address-fontify is run
68;; (say, using font-lock-fontify-buffer), then font-lock face will
69;; override goto-address faces.
70
a69315a1
RS
71;;; Code:
72
73(require 'browse-url)
74
94114394
RS
75(defgroup goto-address nil
76 "Click to browse URL or to send to e-mail address."
77 :group 'mouse
78 :group 'hypermedia)
79
80
e137161b 81;;; I don't expect users to want fontify'ing without highlighting.
94114394 82(defcustom goto-address-fontify-p t
e137161b 83 "*If t, URL's and e-mail addresses in buffer are fontified.
94114394
RS
84But only if `goto-address-highlight-p' is also non-nil."
85 :type 'boolean
86 :group 'goto-address)
e137161b 87
94114394
RS
88(defcustom goto-address-highlight-p t
89 "*If t, URL's and e-mail addresses in buffer are highlighted."
90 :type 'boolean
91 :group 'goto-address)
a69315a1 92
94114394
RS
93(defcustom goto-address-fontify-maximum-size 30000
94 "*Maximum size of file in which to fontify and/or highlight URL's."
95 :type 'integer
96 :group 'goto-address)
a69315a1
RS
97
98(defvar goto-address-mail-regexp
99 "[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+"
100 "A regular expression probably matching an e-mail address.")
101
102(defvar goto-address-url-regexp
103 (concat "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|"
104 "telnet\\|wais\\):\\(//[-a-zA-Z0-9_.]+:"
105 "[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*"
106 "[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
107 "A regular expression probably matching a URL.")
108
94114394 109(defcustom goto-address-mail-method
a69315a1
RS
110 'goto-address-send-using-mail
111 "*Function to compose mail.
112Two pre-made functions are `goto-address-send-using-mail' (sendmail);
94114394
RS
113and `goto-address-send-using-mh-e' (MH-E)."
114 :type 'function
115 :group 'goto-address)
a69315a1 116
7caa5416
KH
117(defvar goto-address-highlight-keymap
118 (let ((m (make-sparse-keymap)))
119 (define-key m [mouse-2] 'goto-address-at-mouse)
120 m)
121 "keymap to hold goto-addr's mouse key defs under highlighted URLs.")
122
94114394
RS
123(defcustom goto-address-url-face 'bold
124 "*Face to use for URLs."
125 :type 'face
126 :group 'goto-address)
127
128(defcustom goto-address-url-mouse-face 'highlight
129 "*Face to use for URLs when the mouse is on them."
130 :type 'face
131 :group 'goto-address)
132
133(defcustom goto-address-mail-face 'italic
134 "*Face to use for e-mail addresses."
135 :type 'face
136 :group 'goto-address)
137
138(defcustom goto-address-mail-mouse-face 'secondary-selection
139 "*Face to use for e-mail addresses when the mouse is on them."
140 :type 'face
141 :group 'goto-address)
8d36235f 142
a69315a1 143(defun goto-address-fontify ()
e137161b
RS
144 "Fontify the URL's and e-mail addresses in the current buffer.
145This function implements `goto-address-highlight-p'
146and `goto-address-fontify-p'."
a69315a1
RS
147 (save-excursion
148 (let ((inhibit-read-only t)
e137161b 149 (inhibit-point-motion-hooks t)
a69315a1
RS
150 (modified (buffer-modified-p)))
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)
79dc9431
RS
155 (let* ((s (match-beginning 0))
156 (e (match-end 0))
157 (this-overlay (make-overlay s e)))
e137161b 158 (and goto-address-fontify-p
79dc9431
RS
159 (overlay-put this-overlay 'face goto-address-url-face))
160 (overlay-put this-overlay
161 'mouse-face goto-address-url-mouse-face)
162 (overlay-put this-overlay
163 'local-map goto-address-highlight-keymap)))
a69315a1
RS
164 (goto-char (point-min))
165 (while (re-search-forward goto-address-mail-regexp nil t)
79dc9431
RS
166 (let* ((s (match-beginning 0))
167 (e (match-end 0))
168 (this-overlay (make-overlay s e)))
e137161b 169 (and goto-address-fontify-p
79dc9431
RS
170 (overlay-put this-overlay 'face goto-address-mail-face))
171 (overlay-put this-overlay 'mouse-face
172 goto-address-mail-mouse-face)
173 (overlay-put this-overlay
174 'local-map goto-address-highlight-keymap)))))
a69315a1
RS
175 (and (buffer-modified-p)
176 (not modified)
177 (set-buffer-modified-p nil)))))
178
a69315a1
RS
179;;; code to find and goto addresses; much of this has been blatantly
180;;; snarfed from browse-url.el
181
e384ec6e 182;;;###autoload
a69315a1
RS
183(defun goto-address-at-mouse (event)
184 "Send to the e-mail address or load the URL clicked with the mouse.
185Send mail to address at position of mouse click. See documentation for
186`goto-address-find-address-at-point'. If no address is found
187there, then load the URL at or before the position of the mouse click."
188 (interactive "e")
189 (save-excursion
190 (let ((posn (event-start event)))
191 (set-buffer (window-buffer (posn-window posn)))
192 (goto-char (posn-point posn))
193 (let ((address
194 (save-excursion (goto-address-find-address-at-point))))
195 (if (string-equal address "")
196 (let ((url (browse-url-url-at-point)))
197 (if (string-equal url "")
198 (error "No e-mail address or URL found")
7f52b124 199 (browse-url url)))
a69315a1
RS
200 (funcall goto-address-mail-method address))))))
201
e384ec6e 202;;;###autoload
a69315a1
RS
203(defun goto-address-at-point ()
204 "Send to the e-mail address or load the URL at point.
205Send mail to address at point. See documentation for
206`goto-address-find-address-at-point'. If no address is found
207there, 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 (string-equal address "")
212 (let ((url (browse-url-url-at-point)))
213 (if (string-equal url "")
214 (error "No e-mail address or URL found")
7f52b124 215 (browse-url url)))
a69315a1
RS
216 (funcall goto-address-mail-method address)))))
217
218(defun goto-address-find-address-at-point ()
219 "Find e-mail address around or before point.
220Then search backwards to beginning of line for the start of an e-mail
221address. If no e-mail address found, return the empty string."
222 (let ((bol (save-excursion (beginning-of-line) (point))))
223 (re-search-backward "[^-_A-z0-9.@]" bol 'lim)
224 (if (or (looking-at goto-address-mail-regexp) ; already at start
225 (let ((eol (save-excursion (end-of-line) (point))))
226 (and (re-search-forward goto-address-mail-regexp eol 'lim)
227 (goto-char (match-beginning 0)))))
228 (buffer-substring (match-beginning 0) (match-end 0))
229 "")))
230
eba8dcd0
RS
231(defun goto-address-send-using-mh-e (to)
232 (require 'mh-comp)
a69315a1
RS
233 (mh-find-path)
234 (let ((cc (mh-read-address "Cc: "))
235 (subject (read-string "Subject: "))
236 (config (current-window-configuration)))
237 (delete-other-windows)
238 (mh-send-sub to cc subject config)))
239
eba8dcd0
RS
240(fset 'goto-address-send-using-mhe 'goto-address-send-using-mh-e)
241
a69315a1
RS
242(defun goto-address-send-using-mail (to)
243 (mail-other-window nil to)
244 (and (goto-char (point-min))
245 (end-of-line 2)))
246
e137161b 247;;;###autoload
a69315a1 248(defun goto-address ()
e137161b
RS
249 "Sets up goto-address functionality in the current buffer.
250Allows user to use mouse/keyboard command to click to go to a URL
251or to send e-mail.
7caa5416 252By default, goto-address binds to mouse-2 and C-c RET.
e137161b
RS
253
254Also fontifies the buffer appropriately (see `goto-address-fontify-p' and
255`goto-address-highlight-p' for more information)."
a69315a1 256 (interactive)
a69315a1 257 (local-set-key "\C-c\r" 'goto-address-at-point)
e137161b 258 (if goto-address-highlight-p
a69315a1
RS
259 (goto-address-fontify)))
260
261(provide 'goto-addr)
262
263;;; goto-addr.el ends here.