More tweaks of skeleton documentation wrt \n behavior at bol/eol.
[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
ba318903 3;; Copyright (C) 1995, 2000-2014 Free Software Foundation, Inc.
8749abea 4
cf6936a4 5;; Author: Eric Ding <ericding@alum.mit.edu>
34dc21db 6;; Maintainer: emacs-devel@gnu.org
8749abea
GM
7;; Created: 15 Aug 1995
8;; Keywords: mh-e, www, mouse, mail
9
10;; This file is part of GNU Emacs.
11
874a927a 12;; GNU Emacs is free software: you can redistribute it and/or modify
8749abea 13;; it under the terms of the GNU General Public License as published by
874a927a
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
8749abea
GM
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
874a927a 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
8749abea
GM
24
25;;; Commentary:
26
27;; This package allows you to click or hit a key sequence while on a
28;; URL or e-mail address, and either load the URL into a browser of
29;; your choice using the browse-url package, or if it's an e-mail
30;; address, to send an e-mail to that address. By default, we bind to
31;; the [mouse-2] and the [C-c return] key sequences.
32
33;; INSTALLATION
34;;
35;; To use goto-address in a particular mode (for example, while
865fe16f 36;; reading mail in mh-e), add this to your init file:
8749abea
GM
37;;
38;; (add-hook 'mh-show-mode-hook 'goto-address)
39;;
28223a7e 40;; The mouse click method is bound to [mouse-2] on highlighted URLs or
8749abea
GM
41;; e-mail addresses only; it functions normally everywhere else. To bind
42;; another mouse click to the function, add the following to your .emacs
43;; (for example):
44;;
45;; (setq goto-address-highlight-keymap
46;; (let ((m (make-sparse-keymap)))
9fd6c528 47;; (define-key m [S-mouse-2] 'goto-address-at-point)
8749abea
GM
48;; m))
49;;
50
8749abea
GM
51;; Known bugs/features:
52;; * goto-address-mail-regexp only catches foo@bar.org style addressing,
53;; not stuff like X.400 addresses, etc.
54;; * regexp also catches Message-Id line, since it is in the format of
55;; an Internet e-mail address (like Compuserve addresses)
51107a70
DL
56;; * If the buffer is fontified after goto-address-fontify is run
57;; (say, using font-lock-fontify-buffer), then font-lock faces will
8749abea
GM
58;; override goto-address faces.
59
60;;; Code:
61
f79538c9
DL
62(require 'thingatpt)
63(autoload 'browse-url-url-at-point "browse-url")
8749abea 64
67ac0f7a
GM
65;; XEmacs needs the following definitions.
66(unless (fboundp 'overlays-in)
67 (require 'overlay))
68(unless (fboundp 'line-beginning-position)
69 (defalias 'line-beginning-position 'point-at-bol))
70(unless (fboundp 'line-end-position)
71 (defalias 'line-end-position 'point-at-eol))
72(unless (fboundp 'match-string-no-properties)
73 (defalias 'match-string-no-properties 'match-string))
74
8749abea
GM
75(defgroup goto-address nil
76 "Click to browse URL or to send to e-mail address."
77 :group 'mouse
26f4b8ab 78 :group 'comm)
8749abea
GM
79
80
d8754ce5 81;; I don't expect users to want fontify'ing without highlighting.
8749abea 82(defcustom goto-address-fontify-p t
fb7ada5f 83 "Non-nil means URLs and e-mail addresses in buffer are fontified.
8749abea
GM
84But only if `goto-address-highlight-p' is also non-nil."
85 :type 'boolean
86 :group 'goto-address)
87
88(defcustom goto-address-highlight-p t
fb7ada5f 89 "Non-nil means URLs and e-mail addresses in buffer are highlighted."
8749abea
GM
90 :type 'boolean
91 :group 'goto-address)
92
93(defcustom goto-address-fontify-maximum-size 30000
fb7ada5f 94 "Maximum size of file in which to fontify and/or highlight URLs.
3e61755b
RS
95A value of t means there is no limit--fontify regardless of the size."
96 :type '(choice (integer :tag "Maximum size") (const :tag "No limit" t))
8749abea
GM
97 :group 'goto-address)
98
99(defvar goto-address-mail-regexp
138399e6 100 ;; Actually pretty much any char could appear in the username part. -stef
02b6dbd8 101 "[-a-zA-Z0-9=._+]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+"
8749abea
GM
102 "A regular expression probably matching an e-mail address.")
103
019f8c1e 104(defvar goto-address-url-regexp
98945794
EZ
105 (concat
106 "\\<\\("
107 (mapconcat 'identity
108 (delete "mailto:"
109 ;; Remove `data:', as it's not terribly useful to follow
110 ;; those. Leaving them causes `use Data::Dumper;' to be
111 ;; fontified oddly in Perl files.
112 (delete "data:"
113 (copy-sequence thing-at-point-uri-schemes)))
114 "\\|")
115 "\\)"
116 thing-at-point-url-path-regexp)
d8754ce5
GM
117 ;; (concat "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|"
118 ;; "telnet\\|wais\\):\\(//[-a-zA-Z0-9_.]+:"
119 ;; "[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*"
120 ;; "[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
8749abea
GM
121 "A regular expression probably matching a URL.")
122
123(defvar goto-address-highlight-keymap
124 (let ((m (make-sparse-keymap)))
9fd6c528 125 (define-key m (if (featurep 'xemacs) (kbd "<button2>") (kbd "<mouse-2>"))
64981116 126 'goto-address-at-point)
67ac0f7a 127 (define-key m (kbd "C-c RET") 'goto-address-at-point)
8749abea 128 m)
79c4a646 129 "Keymap to hold goto-addr's mouse key defs under highlighted URLs.")
8749abea 130
58cb49d4 131(defcustom goto-address-url-face 'link
f79538c9 132 "Face to use for URLs."
8749abea
GM
133 :type 'face
134 :group 'goto-address)
135
136(defcustom goto-address-url-mouse-face 'highlight
f79538c9 137 "Face to use for URLs when the mouse is on them."
8749abea
GM
138 :type 'face
139 :group 'goto-address)
140
141(defcustom goto-address-mail-face 'italic
f79538c9 142 "Face to use for e-mail addresses."
8749abea
GM
143 :type 'face
144 :group 'goto-address)
145
146(defcustom goto-address-mail-mouse-face 'secondary-selection
f79538c9 147 "Face to use for e-mail addresses when the mouse is on them."
8749abea
GM
148 :type 'face
149 :group 'goto-address)
150
2cbee4c5
GM
151(defun goto-address-unfontify (start end)
152 "Remove `goto-address' fontification from the given region."
153 (dolist (overlay (overlays-in start end))
154 (if (overlay-get overlay 'goto-address)
155 (delete-overlay overlay))))
156
8b026efe
GM
157(defvar goto-address-prog-mode)
158
213ac168 159(defun goto-address-fontify (&optional start end)
28223a7e 160 "Fontify the URLs and e-mail addresses in the current buffer.
8749abea
GM
161This function implements `goto-address-highlight-p'
162and `goto-address-fontify-p'."
51107a70 163 ;; Clean up from any previous go.
213ac168 164 (goto-address-unfontify (or start (point-min)) (or end (point-max)))
8749abea 165 (save-excursion
51107a70 166 (let ((inhibit-point-motion-hooks t))
213ac168 167 (goto-char (or start (point-min)))
2cbee4c5 168 (when (or (eq t goto-address-fontify-maximum-size)
213ac168
SM
169 (< (- (or end (point-max)) (point))
170 goto-address-fontify-maximum-size))
171 (while (re-search-forward goto-address-url-regexp end t)
2cbee4c5
GM
172 (let* ((s (match-beginning 0))
173 (e (match-end 0))
174 this-overlay)
175 (when (or (not goto-address-prog-mode)
176 ;; This tests for both comment and string
177 ;; syntax.
178 (nth 8 (syntax-ppss)))
179 (setq this-overlay (make-overlay s e))
180 (and goto-address-fontify-p
181 (overlay-put this-overlay 'face goto-address-url-face))
182 (overlay-put this-overlay 'evaporate t)
183 (overlay-put this-overlay
184 'mouse-face goto-address-url-mouse-face)
185 (overlay-put this-overlay 'follow-link t)
186 (overlay-put this-overlay
187 'help-echo "mouse-2, C-c RET: follow URL")
188 (overlay-put this-overlay
189 'keymap goto-address-highlight-keymap)
190 (overlay-put this-overlay 'goto-address t))))
213ac168
SM
191 (goto-char (or start (point-min)))
192 (while (re-search-forward goto-address-mail-regexp end t)
2cbee4c5
GM
193 (let* ((s (match-beginning 0))
194 (e (match-end 0))
195 this-overlay)
196 (when (or (not goto-address-prog-mode)
197 ;; This tests for both comment and string
198 ;; syntax.
199 (nth 8 (syntax-ppss)))
200 (setq this-overlay (make-overlay s e))
201 (and goto-address-fontify-p
202 (overlay-put this-overlay 'face goto-address-mail-face))
203 (overlay-put this-overlay 'evaporate t)
204 (overlay-put this-overlay 'mouse-face
205 goto-address-mail-mouse-face)
206 (overlay-put this-overlay 'follow-link t)
207 (overlay-put this-overlay
208 'help-echo "mouse-2, C-c RET: mail this address")
209 (overlay-put this-overlay
210 'keymap goto-address-highlight-keymap)
211 (overlay-put this-overlay 'goto-address t))))))))
212
213(defun goto-address-fontify-region (start end)
214 "Fontify URLs and e-mail addresses in the given region."
215 (save-excursion
213ac168
SM
216 (let ((beg-line (progn (goto-char start) (line-beginning-position)))
217 (end-line (progn (goto-char end) (line-end-position))))
218 (goto-address-fontify beg-line end-line))))
8749abea 219
d8754ce5
GM
220;; code to find and goto addresses; much of this has been blatantly
221;; snarfed from browse-url.el
8749abea
GM
222
223;;;###autoload
9fd6c528
SM
224(define-obsolete-function-alias
225 'goto-address-at-mouse 'goto-address-at-point "22.1")
8749abea
GM
226
227;;;###autoload
9fd6c528 228(defun goto-address-at-point (&optional event)
8749abea
GM
229 "Send to the e-mail address or load the URL at point.
230Send mail to address at point. See documentation for
231`goto-address-find-address-at-point'. If no address is found
232there, then load the URL at or before point."
9fd6c528 233 (interactive (list last-input-event))
8749abea 234 (save-excursion
43fb082d 235 (if event (posn-set-point (event-end event)))
8749abea 236 (let ((address (save-excursion (goto-address-find-address-at-point))))
a1506d29 237 (if (and address
1c66d9fb
GM
238 (save-excursion
239 (goto-char (previous-single-char-property-change
240 (point) 'goto-address nil
241 (line-beginning-position)))
242 (not (looking-at goto-address-url-regexp))))
f79538c9
DL
243 (compose-mail address)
244 (let ((url (browse-url-url-at-point)))
245 (if url
246 (browse-url url)
247 (error "No e-mail address or URL found")))))))
8749abea
GM
248
249(defun goto-address-find-address-at-point ()
250 "Find e-mail address around or before point.
251Then search backwards to beginning of line for the start of an e-mail
f79538c9
DL
252address. If no e-mail address found, return nil."
253 (re-search-backward "[^-_A-z0-9.@]" (line-beginning-position) 'lim)
254 (if (or (looking-at goto-address-mail-regexp) ; already at start
255 (and (re-search-forward goto-address-mail-regexp
256 (line-end-position) 'lim)
257 (goto-char (match-beginning 0))))
258 (match-string-no-properties 0)))
8749abea
GM
259
260;;;###autoload
261(defun goto-address ()
262 "Sets up goto-address functionality in the current buffer.
263Allows user to use mouse/keyboard command to click to go to a URL
264or to send e-mail.
79c4a646
JL
265By default, goto-address binds `goto-address-at-point' to mouse-2 and C-c RET
266only on URLs and e-mail addresses.
8749abea
GM
267
268Also fontifies the buffer appropriately (see `goto-address-fontify-p' and
269`goto-address-highlight-p' for more information)."
270 (interactive)
8749abea
GM
271 (if goto-address-highlight-p
272 (goto-address-fontify)))
0d61de90 273;;;###autoload(put 'goto-address 'safe-local-eval-function t)
8749abea 274
2cbee4c5
GM
275;;;###autoload
276(define-minor-mode goto-address-mode
e1ac4066
GM
277 "Minor mode to buttonize URLs and e-mail addresses in the current buffer.
278With a prefix argument ARG, enable the mode if ARG is positive,
279and disable it otherwise. If called from Lisp, enable the mode
280if ARG is omitted or nil."
2cbee4c5
GM
281 nil
282 ""
283 nil
284 (if goto-address-mode
285 (jit-lock-register #'goto-address-fontify-region)
286 (jit-lock-unregister #'goto-address-fontify-region)
287 (save-restriction
288 (widen)
289 (goto-address-unfontify (point-min) (point-max)))))
290
291;;;###autoload
292(define-minor-mode goto-address-prog-mode
ac6c8639 293 "Like `goto-address-mode', but only for comments and strings."
2cbee4c5
GM
294 nil
295 ""
296 nil
297 (if goto-address-prog-mode
298 (jit-lock-register #'goto-address-fontify-region)
299 (jit-lock-unregister #'goto-address-fontify-region)
300 (save-restriction
301 (widen)
302 (goto-address-unfontify (point-min) (point-max)))))
303
8749abea
GM
304(provide 'goto-addr)
305
55535639 306;;; goto-addr.el ends here