Initial Commit
[packages] / xemacs-packages / fsf-compat / 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 Free Software Foundation, Inc.
4
5 ;; Author: Eric Ding <ericding@alum.mit.edu>
6 ;; Maintainer: FSF
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 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):
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 ;; 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.
61
62 ;;; Code:
63
64 (require 'thingatpt)
65 (autoload 'browse-url-url-at-point "browse-url")
66
67 ;; XEmacs needs the following definitions.
68 (if (featurep 'xemacs)
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
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
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)
89
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)
94
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)
99
100 (defvar goto-address-mail-regexp
101   "[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+"
102   "A regular expression probably matching an e-mail address.")
103
104 (defvar goto-address-url-regexp thing-at-point-url-regexp
105 ;;;   (concat "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|"
106 ;;;       "telnet\\|wais\\):\\(//[-a-zA-Z0-9_.]+:"
107 ;;;       "[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*"
108 ;;;       "[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
109   "A regular expression probably matching a URL.")
110
111 (defvar goto-address-highlight-keymap
112   (let ((m (make-sparse-keymap)))
113     (if (featurep 'xemacs)
114         (define-key m (kbd "<button2>") 'goto-address-at-mouse)
115       (define-key m (kbd "<mouse-2>") 'goto-address-at-mouse))
116     (define-key m (kbd "C-c RET") 'goto-address-at-point)
117     m)
118   "keymap to hold goto-addr's mouse key defs under highlighted URLs.")
119
120 (defcustom goto-address-url-face 'bold
121   "Face to use for URLs."
122   :type 'face
123   :group 'goto-address)
124
125 (defcustom goto-address-url-mouse-face 'highlight
126   "Face to use for URLs when the mouse is on them."
127   :type 'face
128   :group 'goto-address)
129
130 (defcustom goto-address-mail-face 'italic
131   "Face to use for e-mail addresses."
132   :type 'face
133   :group 'goto-address)
134
135 (defcustom goto-address-mail-mouse-face 'secondary-selection
136   "Face to use for e-mail addresses when the mouse is on them."
137   :type 'face
138   :group 'goto-address)
139
140 (defun goto-address-fontify ()
141   "Fontify the URLs and e-mail addresses in the current buffer.
142 This function implements `goto-address-highlight-p'
143 and `goto-address-fontify-p'."
144   ;; Clean up from any previous go.
145   (dolist (overlay (overlays-in (point-min) (point-max)))
146     (if (overlay-get overlay 'goto-address)
147         (delete-overlay overlay)))
148   (save-excursion
149     (let ((inhibit-point-motion-hooks t))
150       (goto-char (point-min))
151       (if (< (- (point-max) (point)) goto-address-fontify-maximum-size)
152           (progn
153             (while (re-search-forward goto-address-url-regexp nil t)
154               (let* ((s (match-beginning 0))
155                      (e (match-end 0))
156                      (this-overlay (make-overlay s e)))
157                 (and goto-address-fontify-p
158                      (overlay-put this-overlay 'face goto-address-url-face))
159                 (overlay-put this-overlay
160                              'mouse-face goto-address-url-mouse-face)
161                 (overlay-put this-overlay
162                              'help-echo "mouse-2: follow URL")
163                 (overlay-put this-overlay
164                              'keymap goto-address-highlight-keymap)
165                 (overlay-put this-overlay 'goto-address t)))
166             (goto-char (point-min))
167             (while (re-search-forward goto-address-mail-regexp nil t)
168               (let* ((s (match-beginning 0))
169                      (e (match-end 0))
170                      (this-overlay (make-overlay s e)))
171                 (and goto-address-fontify-p
172                      (overlay-put this-overlay 'face goto-address-mail-face))
173                 (overlay-put this-overlay 'mouse-face
174                              goto-address-mail-mouse-face)
175                 (overlay-put this-overlay
176                              'help-echo "mouse-2: mail this address")
177                 (overlay-put this-overlay
178                              'keymap goto-address-highlight-keymap)
179                 (overlay-put this-overlay 'goto-address t))))))))
180
181 ;;; code to find and goto addresses; much of this has been blatantly
182 ;;; snarfed from browse-url.el
183
184 ;;;###autoload
185 (defun goto-address-at-mouse (event)
186   "Send to the e-mail address or load the URL clicked with the mouse.
187 Send mail to address at position of mouse click.  See documentation for
188 `goto-address-find-address-at-point'.  If no address is found
189 there, then load the URL at or before the position of the mouse click."
190   (interactive "e")
191   (save-excursion
192     (mouse-set-point event)
193     (goto-address-at-point)))
194
195 ;;;###autoload
196 (defun goto-address-at-point ()
197   "Send to the e-mail address or load the URL at point.
198 Send mail to address at point.  See documentation for
199 `goto-address-find-address-at-point'.  If no address is found
200 there, then load the URL at or before point."
201   (interactive)
202   (save-excursion
203     (let ((address (save-excursion (goto-address-find-address-at-point))))
204       (if address
205           (compose-mail address)
206         (let ((url (browse-url-url-at-point)))
207           (if url
208               (browse-url url)
209             (error "No e-mail address or URL found")))))))
210
211 (defun goto-address-find-address-at-point ()
212   "Find e-mail address around or before point.
213 Then search backwards to beginning of line for the start of an e-mail
214 address.  If no e-mail address found, return nil."
215   (re-search-backward "[^-_A-z0-9.@]" (line-beginning-position) 'lim)
216   (if (or (looking-at goto-address-mail-regexp) ; already at start
217           (and (re-search-forward goto-address-mail-regexp
218                                   (line-end-position) 'lim)
219                (goto-char (match-beginning 0))))
220       (match-string-no-properties 0)))
221
222 ;;;###autoload
223 (defun goto-address ()
224   "Sets up goto-address functionality in the current buffer.
225 Allows user to use mouse/keyboard command to click to go to a URL
226 or to send e-mail.
227 By default, goto-address binds to mouse-2 and C-c RET.
228
229 Also fontifies the buffer appropriately (see `goto-address-fontify-p' and
230 `goto-address-highlight-p' for more information)."
231   (interactive)
232   (if goto-address-highlight-p
233       (goto-address-fontify)))
234
235 (provide 'goto-addr)
236
237 ;;; goto-addr.el ends here.