Commit | Line | Data |
---|---|---|
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 | ;; | |
45 | ;; (setq goto-address-mail-method 'goto-address-send-using-mhe) | |
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 | ||
e137161b | 75 | ;;; I don't expect users to want fontify'ing without highlighting. |
a69315a1 | 76 | (defvar goto-address-fontify-p t |
e137161b RS |
77 | "*If t, URL's and e-mail addresses in buffer are fontified. |
78 | But only if `goto-address-highlight-p' is also non-nil.") | |
79 | ||
80 | (defvar goto-address-highlight-p t | |
81 | "*If t, URL's and e-mail addresses in buffer are highlighted.") | |
a69315a1 RS |
82 | |
83 | (defvar goto-address-fontify-maximum-size 30000 | |
e137161b | 84 | "*Maximum size of file in which to fontify and/or highlight URL's.") |
a69315a1 RS |
85 | |
86 | (defvar goto-address-mail-regexp | |
87 | "[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" | |
88 | "A regular expression probably matching an e-mail address.") | |
89 | ||
90 | (defvar goto-address-url-regexp | |
91 | (concat "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|" | |
92 | "telnet\\|wais\\):\\(//[-a-zA-Z0-9_.]+:" | |
93 | "[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*" | |
94 | "[-a-zA-Z0-9_=#$@~`%&*+|\\/]") | |
95 | "A regular expression probably matching a URL.") | |
96 | ||
97 | (defvar goto-address-mail-method | |
98 | 'goto-address-send-using-mail | |
99 | "*Function to compose mail. | |
100 | Two pre-made functions are `goto-address-send-using-mail' (sendmail); | |
101 | and `goto-address-send-using-mhe' (MH-E).") | |
102 | ||
7caa5416 KH |
103 | (defvar goto-address-highlight-keymap |
104 | (let ((m (make-sparse-keymap))) | |
105 | (define-key m [mouse-2] 'goto-address-at-mouse) | |
106 | m) | |
107 | "keymap to hold goto-addr's mouse key defs under highlighted URLs.") | |
108 | ||
a69315a1 | 109 | (defun goto-address-fontify () |
e137161b RS |
110 | "Fontify the URL's and e-mail addresses in the current buffer. |
111 | This function implements `goto-address-highlight-p' | |
112 | and `goto-address-fontify-p'." | |
a69315a1 RS |
113 | (save-excursion |
114 | (let ((inhibit-read-only t) | |
e137161b | 115 | (inhibit-point-motion-hooks t) |
a69315a1 RS |
116 | (modified (buffer-modified-p))) |
117 | (goto-char (point-min)) | |
118 | (if (< (- (point-max) (point)) goto-address-fontify-maximum-size) | |
119 | (progn | |
120 | (while (re-search-forward goto-address-url-regexp nil t) | |
7caa5416 KH |
121 | (let ((s (match-beginning 0)) |
122 | (e (match-end 0))) | |
123 | (goto-char e) | |
e137161b | 124 | (and goto-address-fontify-p |
7caa5416 KH |
125 | (put-text-property s e 'face 'bold)) |
126 | (put-text-property s e 'mouse-face 'highlight) | |
127 | (put-text-property | |
128 | s e 'local-map goto-address-highlight-keymap))) | |
a69315a1 RS |
129 | (goto-char (point-min)) |
130 | (while (re-search-forward goto-address-mail-regexp nil t) | |
7caa5416 KH |
131 | (let ((s (match-beginning 0)) |
132 | (e (match-end 0))) | |
e137161b RS |
133 | (goto-char (match-end 0)) |
134 | (and goto-address-fontify-p | |
a69315a1 | 135 | (put-text-property (match-beginning 0) (match-end 0) |
e137161b RS |
136 | 'face 'italic)) |
137 | (put-text-property (match-beginning 0) (match-end 0) | |
7caa5416 KH |
138 | 'mouse-face 'secondary-selection) |
139 | (put-text-property | |
140 | s e 'local-map goto-address-highlight-keymap))))) | |
a69315a1 RS |
141 | (and (buffer-modified-p) |
142 | (not modified) | |
143 | (set-buffer-modified-p nil))))) | |
144 | ||
a69315a1 RS |
145 | ;;; code to find and goto addresses; much of this has been blatantly |
146 | ;;; snarfed from browse-url.el | |
147 | ||
148 | (defun goto-address-at-mouse (event) | |
149 | "Send to the e-mail address or load the URL clicked with the mouse. | |
150 | Send mail to address at position of mouse click. See documentation for | |
151 | `goto-address-find-address-at-point'. If no address is found | |
152 | there, then load the URL at or before the position of the mouse click." | |
153 | (interactive "e") | |
154 | (save-excursion | |
155 | (let ((posn (event-start event))) | |
156 | (set-buffer (window-buffer (posn-window posn))) | |
157 | (goto-char (posn-point posn)) | |
158 | (let ((address | |
159 | (save-excursion (goto-address-find-address-at-point)))) | |
160 | (if (string-equal address "") | |
161 | (let ((url (browse-url-url-at-point))) | |
162 | (if (string-equal url "") | |
163 | (error "No e-mail address or URL found") | |
164 | (funcall browse-url-browser-function url))) | |
165 | (funcall goto-address-mail-method address)))))) | |
166 | ||
167 | (defun goto-address-at-point () | |
168 | "Send to the e-mail address or load the URL at point. | |
169 | Send mail to address at point. See documentation for | |
170 | `goto-address-find-address-at-point'. If no address is found | |
171 | there, then load the URL at or before point." | |
172 | (interactive) | |
173 | (save-excursion | |
174 | (let ((address (save-excursion (goto-address-find-address-at-point)))) | |
175 | (if (string-equal address "") | |
176 | (let ((url (browse-url-url-at-point))) | |
177 | (if (string-equal url "") | |
178 | (error "No e-mail address or URL found") | |
179 | (funcall browse-url-browser-function url))) | |
180 | (funcall goto-address-mail-method address))))) | |
181 | ||
182 | (defun goto-address-find-address-at-point () | |
183 | "Find e-mail address around or before point. | |
184 | Then search backwards to beginning of line for the start of an e-mail | |
185 | address. If no e-mail address found, return the empty string." | |
186 | (let ((bol (save-excursion (beginning-of-line) (point)))) | |
187 | (re-search-backward "[^-_A-z0-9.@]" bol 'lim) | |
188 | (if (or (looking-at goto-address-mail-regexp) ; already at start | |
189 | (let ((eol (save-excursion (end-of-line) (point)))) | |
190 | (and (re-search-forward goto-address-mail-regexp eol 'lim) | |
191 | (goto-char (match-beginning 0))))) | |
192 | (buffer-substring (match-beginning 0) (match-end 0)) | |
193 | ""))) | |
194 | ||
195 | (defun goto-address-send-using-mhe (to) | |
196 | (mh-find-path) | |
197 | (let ((cc (mh-read-address "Cc: ")) | |
198 | (subject (read-string "Subject: ")) | |
199 | (config (current-window-configuration))) | |
200 | (delete-other-windows) | |
201 | (mh-send-sub to cc subject config))) | |
202 | ||
203 | (defun goto-address-send-using-mail (to) | |
204 | (mail-other-window nil to) | |
205 | (and (goto-char (point-min)) | |
206 | (end-of-line 2))) | |
207 | ||
e137161b | 208 | ;;;###autoload |
a69315a1 | 209 | (defun goto-address () |
e137161b RS |
210 | "Sets up goto-address functionality in the current buffer. |
211 | Allows user to use mouse/keyboard command to click to go to a URL | |
212 | or to send e-mail. | |
7caa5416 | 213 | By default, goto-address binds to mouse-2 and C-c RET. |
e137161b RS |
214 | |
215 | Also fontifies the buffer appropriately (see `goto-address-fontify-p' and | |
216 | `goto-address-highlight-p' for more information)." | |
a69315a1 | 217 | (interactive) |
a69315a1 | 218 | (local-set-key "\C-c\r" 'goto-address-at-point) |
e137161b | 219 | (if goto-address-highlight-p |
a69315a1 RS |
220 | (goto-address-fontify))) |
221 | ||
222 | (provide 'goto-addr) | |
223 | ||
224 | ;;; goto-addr.el ends here. |