* etags.c (lowcase): Use the standard tolower function.
[emacs.git] / lisp / goto-addr.el
blobf338cdcf10e021ea5a704f10268f2f6d804cd932
1 ;;; goto-addr.el --- click to browse URL or to send to e-mail address
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Maintainer: Eric Ding <ericding@mit.edu>
5 ;; Created: 15 Aug 1995
6 ;; Keywords: mh-e, www, mouse, mail
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;;; Commentary:
26 ;; This package allows you to click or hit a key sequence while on a
27 ;; URL or e-mail address, and either load the URL into a browser of
28 ;; your choice using the browse-url package, or if it's an e-mail
29 ;; address, to send an e-mail to that address. By default, we bind to
30 ;; the [S-mouse-1] and the [C-c return] key sequences.
32 ;; You will also need the browse-url.el package to use goto-address.
33 ;; You can find it at <URL:http://wombat.doc.ic.ac.uk/emacs/browse-url.el>.
35 ;; INSTALLATION
37 ;; To install goto-address, put goto-addr.el somewhere in
38 ;; your load-path and add the following to your .emacs file:
40 ;; (autoload 'goto-address "goto-addr"
41 ;; "Set up buffer to click to browse URL or to send to e-mail address" t)
43 ;; To use it in a particular mode (for example, while reading mail in
44 ;; mh-e), add something like this in your .emacs file:
45 ;;
46 ;; (add-hook 'mh-show-mode-hook 'goto-address)
48 ;; By default, goto-address now sends using `mail' instead of `mh-send'.
49 ;; To use mh-e to send mail, add the following to your .emacs file:
51 ;; (setq goto-address-mail-method 'goto-address-send-using-mhe)
53 ;; To rebind, for example, the mouse click method to [mouse-2] in
54 ;; mh-show-mode, add the following (instead of the first add-hook example
55 ;; above) to your .emacs file:
57 ;; (defun my-goto-address ()
58 ;; (goto-address)
59 ;; (local-unset-key [S-mouse-1])
60 ;; (local-set-key [mouse-2] 'goto-address-at-mouse))
62 ;; (add-hook 'mh-show-mode-hook 'my-goto-address)
64 ;; [mouse-2] is not the default mouse binding because I use goto-address in
65 ;; some editable buffers, where [mouse-2] means mouse-yank-at-click, as well
66 ;; as in some modes where [mouse-2] is bound to other useful functions.
68 ;; BUG REPORTS
70 ;; Please send bug reports to me at ericding@mit.edu.
72 ;; Known bugs/features:
73 ;; * goto-address-mail-regexp only catches foo@bar.org style addressing,
74 ;; not stuff like X.400 addresses, etc.
75 ;; * regexp also catches Message-Id line, since it is in the format of
76 ;; an Internet e-mail address (like Compuserve addresses)
77 ;; * If show buffer is fontified after goto-address-fontify is run
78 ;; (say, using font-lock-fontify-buffer), then font-lock face will
79 ;; override goto-address faces.
81 ;;; Change log:
83 ;;; Code:
85 (require 'browse-url)
87 (defvar goto-address-fontify-p t
88 "*If t, URL's and e-mail address in buffer are fontified.")
90 (defvar goto-address-fontify-maximum-size 30000
91 "*Maximum size of file in which to fontify URL's.")
93 (defvar goto-address-mail-regexp
94 "[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+"
95 "A regular expression probably matching an e-mail address.")
97 (defvar goto-address-url-regexp
98 (concat "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|"
99 "telnet\\|wais\\):\\(//[-a-zA-Z0-9_.]+:"
100 "[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*"
101 "[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
102 "A regular expression probably matching a URL.")
104 (defvar goto-address-mail-method
105 'goto-address-send-using-mail
106 "*Function to compose mail.
107 Two pre-made functions are `goto-address-send-using-mail' (sendmail);
108 and `goto-address-send-using-mhe' (MH-E).")
110 (defun goto-address-fontify ()
111 "Fontify the URL's and e-mail addresses in the current buffer."
112 (save-excursion
113 (let ((inhibit-read-only t)
114 (modified (buffer-modified-p)))
115 (goto-char (point-min))
116 (if (< (- (point-max) (point)) goto-address-fontify-maximum-size)
117 (progn
118 (while (re-search-forward goto-address-url-regexp nil t)
119 ;; if text is invisible, we ignore it
120 (and (goto-address-skip-invisible (match-beginning 0))
121 (progn
122 (goto-char (match-end 0))
123 (put-text-property (match-beginning 0) (match-end 0)
124 'face 'bold)
125 (put-text-property (match-beginning 0) (match-end 0)
126 'mouse-face 'highlight))))
127 (goto-char (point-min))
128 (while (re-search-forward goto-address-mail-regexp nil t)
129 ;; if text is invisible, we ignore it
130 (and (goto-address-skip-invisible (match-beginning 0))
131 (progn
132 (goto-char (match-end 0))
133 (put-text-property (match-beginning 0) (match-end 0)
134 'face 'italic)
135 (put-text-property (match-beginning 0) (match-end 0)
136 'mouse-face 'secondary-selection))))))
137 (and (buffer-modified-p)
138 (not modified)
139 (set-buffer-modified-p nil)))))
141 (defun goto-address-skip-invisible (char)
142 "If char is not invisible, then return t. Otherwise, move forward in buffer
143 until a non-invisible char is found, goto that position, and return nil."
144 (if (get-text-property char 'invisible)
145 (let ((char (1+ char)))
146 (while (get-text-property char 'invisible)
147 (setq char (1+ char))
148 (goto-char char)))
151 ;;; code to find and goto addresses; much of this has been blatantly
152 ;;; snarfed from browse-url.el
154 (defun goto-address-at-mouse (event)
155 "Send to the e-mail address or load the URL clicked with the mouse.
156 Send mail to address at position of mouse click. See documentation for
157 `goto-address-find-address-at-point'. If no address is found
158 there, then load the URL at or before the position of the mouse click."
159 (interactive "e")
160 (save-excursion
161 (let ((posn (event-start event)))
162 (set-buffer (window-buffer (posn-window posn)))
163 (goto-char (posn-point posn))
164 (let ((address
165 (save-excursion (goto-address-find-address-at-point))))
166 (if (string-equal address "")
167 (let ((url (browse-url-url-at-point)))
168 (if (string-equal url "")
169 (error "No e-mail address or URL found")
170 (funcall browse-url-browser-function url)))
171 (funcall goto-address-mail-method address))))))
173 (defun goto-address-at-point ()
174 "Send to the e-mail address or load the URL at point.
175 Send mail to address at point. See documentation for
176 `goto-address-find-address-at-point'. If no address is found
177 there, then load the URL at or before point."
178 (interactive)
179 (save-excursion
180 (let ((address (save-excursion (goto-address-find-address-at-point))))
181 (if (string-equal address "")
182 (let ((url (browse-url-url-at-point)))
183 (if (string-equal url "")
184 (error "No e-mail address or URL found")
185 (funcall browse-url-browser-function url)))
186 (funcall goto-address-mail-method address)))))
188 (defun goto-address-find-address-at-point ()
189 "Find e-mail address around or before point.
190 Then search backwards to beginning of line for the start of an e-mail
191 address. If no e-mail address found, return the empty string."
192 (let ((bol (save-excursion (beginning-of-line) (point))))
193 (re-search-backward "[^-_A-z0-9.@]" bol 'lim)
194 (if (or (looking-at goto-address-mail-regexp) ; already at start
195 (let ((eol (save-excursion (end-of-line) (point))))
196 (and (re-search-forward goto-address-mail-regexp eol 'lim)
197 (goto-char (match-beginning 0)))))
198 (buffer-substring (match-beginning 0) (match-end 0))
199 "")))
201 (defun goto-address-send-using-mhe (to)
202 (mh-find-path)
203 (let ((cc (mh-read-address "Cc: "))
204 (subject (read-string "Subject: "))
205 (config (current-window-configuration)))
206 (delete-other-windows)
207 (mh-send-sub to cc subject config)))
209 (defun goto-address-send-using-mail (to)
210 (mail-other-window nil to)
211 (and (goto-char (point-min))
212 (end-of-line 2)))
214 (defun goto-address ()
215 (interactive)
216 (local-set-key [S-mouse-1] 'goto-address-at-mouse)
217 (local-set-key "\C-c\r" 'goto-address-at-point)
218 (if goto-address-fontify-p
219 (goto-address-fontify)))
221 (provide 'goto-addr)
223 ;;; goto-addr.el ends here.