2000-03-20 13:12:14 +00:00
|
|
|
;;; goto-addr.el --- click to browse URL or to send to e-mail address
|
|
|
|
|
2001-04-23 10:16:54 +00:00
|
|
|
;; Copyright (C) 1995, 2000, 2001 Free Software Foundation, Inc.
|
2000-03-20 13:12:14 +00:00
|
|
|
|
2000-07-13 19:01:10 +00:00
|
|
|
;; Author: Eric Ding <ericding@alum.mit.edu>
|
2000-07-18 12:58:36 +00:00
|
|
|
;; Maintainer: FSF
|
2000-03-20 13:12:14 +00:00
|
|
|
;; Created: 15 Aug 1995
|
|
|
|
;; Keywords: mh-e, www, mouse, mail
|
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
;; any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
;; Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; This package allows you to click or hit a key sequence while on a
|
|
|
|
;; URL or e-mail address, and either load the URL into a browser of
|
|
|
|
;; your choice using the browse-url package, or if it's an e-mail
|
|
|
|
;; address, to send an e-mail to that address. By default, we bind to
|
|
|
|
;; the [mouse-2] and the [C-c return] key sequences.
|
|
|
|
|
|
|
|
;; INSTALLATION
|
|
|
|
;;
|
|
|
|
;; To use goto-address in a particular mode (for example, while
|
|
|
|
;; reading mail in mh-e), add something like this in your .emacs file:
|
|
|
|
;;
|
|
|
|
;; (add-hook 'mh-show-mode-hook 'goto-address)
|
|
|
|
;;
|
2000-06-15 11:43:01 +00:00
|
|
|
;; The mouse click method is bound to [mouse-2] on highlighted URLs or
|
2000-03-20 13:12:14 +00:00
|
|
|
;; e-mail addresses only; it functions normally everywhere else. To bind
|
|
|
|
;; another mouse click to the function, add the following to your .emacs
|
|
|
|
;; (for example):
|
|
|
|
;;
|
|
|
|
;; (setq goto-address-highlight-keymap
|
|
|
|
;; (let ((m (make-sparse-keymap)))
|
|
|
|
;; (define-key m [S-mouse-2] 'goto-address-at-mouse)
|
|
|
|
;; m))
|
|
|
|
;;
|
|
|
|
|
|
|
|
;; Known bugs/features:
|
|
|
|
;; * goto-address-mail-regexp only catches foo@bar.org style addressing,
|
|
|
|
;; not stuff like X.400 addresses, etc.
|
|
|
|
;; * regexp also catches Message-Id line, since it is in the format of
|
|
|
|
;; an Internet e-mail address (like Compuserve addresses)
|
2000-07-20 17:29:46 +00:00
|
|
|
;; * If the buffer is fontified after goto-address-fontify is run
|
|
|
|
;; (say, using font-lock-fontify-buffer), then font-lock faces will
|
2000-03-20 13:12:14 +00:00
|
|
|
;; override goto-address faces.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2000-08-16 21:28:35 +00:00
|
|
|
(require 'thingatpt)
|
|
|
|
(autoload 'browse-url-url-at-point "browse-url")
|
2000-03-20 13:12:14 +00:00
|
|
|
|
2001-04-23 10:16:54 +00:00
|
|
|
;; XEmacs needs the following definitions.
|
|
|
|
(unless (fboundp 'overlays-in)
|
|
|
|
(require 'overlay))
|
|
|
|
(unless (fboundp 'line-beginning-position)
|
|
|
|
(defalias 'line-beginning-position 'point-at-bol))
|
|
|
|
(unless (fboundp 'line-end-position)
|
|
|
|
(defalias 'line-end-position 'point-at-eol))
|
|
|
|
(unless (fboundp 'match-string-no-properties)
|
|
|
|
(defalias 'match-string-no-properties 'match-string))
|
|
|
|
|
2000-03-20 13:12:14 +00:00
|
|
|
(defgroup goto-address nil
|
|
|
|
"Click to browse URL or to send to e-mail address."
|
|
|
|
:group 'mouse
|
|
|
|
:group 'hypermedia)
|
|
|
|
|
|
|
|
|
2001-10-05 09:36:02 +00:00
|
|
|
;; I don't expect users to want fontify'ing without highlighting.
|
2000-03-20 13:12:14 +00:00
|
|
|
(defcustom goto-address-fontify-p t
|
2000-08-16 21:28:35 +00:00
|
|
|
"*Non-nil means URLs and e-mail addresses in buffer are fontified.
|
2000-03-20 13:12:14 +00:00
|
|
|
But only if `goto-address-highlight-p' is also non-nil."
|
|
|
|
:type 'boolean
|
|
|
|
:group 'goto-address)
|
|
|
|
|
|
|
|
(defcustom goto-address-highlight-p t
|
2000-08-16 21:28:35 +00:00
|
|
|
"*Non-nil means URLs and e-mail addresses in buffer are highlighted."
|
2000-03-20 13:12:14 +00:00
|
|
|
:type 'boolean
|
|
|
|
:group 'goto-address)
|
|
|
|
|
|
|
|
(defcustom goto-address-fontify-maximum-size 30000
|
2000-06-15 11:43:01 +00:00
|
|
|
"*Maximum size of file in which to fontify and/or highlight URLs."
|
2000-03-20 13:12:14 +00:00
|
|
|
:type 'integer
|
|
|
|
:group 'goto-address)
|
|
|
|
|
|
|
|
(defvar goto-address-mail-regexp
|
2001-10-11 23:40:35 +00:00
|
|
|
;; Actually pretty much any char could appear in the username part. -stef
|
|
|
|
"[-a-zA-Z0-9._+]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+"
|
2000-03-20 13:12:14 +00:00
|
|
|
"A regular expression probably matching an e-mail address.")
|
|
|
|
|
2002-10-29 18:12:33 +00:00
|
|
|
(defvar goto-address-url-regexp
|
|
|
|
(concat "\\<\\("
|
|
|
|
(mapconcat 'identity
|
|
|
|
(delete "mailto:" (copy-sequence thing-at-point-uri-schemes))
|
|
|
|
"\\|")
|
|
|
|
"\\)"
|
|
|
|
thing-at-point-url-path-regexp)
|
2001-10-05 09:36:02 +00:00
|
|
|
;; (concat "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|"
|
|
|
|
;; "telnet\\|wais\\):\\(//[-a-zA-Z0-9_.]+:"
|
|
|
|
;; "[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*"
|
|
|
|
;; "[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
|
2000-03-20 13:12:14 +00:00
|
|
|
"A regular expression probably matching a URL.")
|
|
|
|
|
|
|
|
(defvar goto-address-highlight-keymap
|
|
|
|
(let ((m (make-sparse-keymap)))
|
2001-04-23 10:16:54 +00:00
|
|
|
(if (featurep 'xemacs)
|
|
|
|
(define-key m (kbd "<button2>") 'goto-address-at-mouse)
|
|
|
|
(define-key m (kbd "<mouse-2>") 'goto-address-at-mouse))
|
|
|
|
(define-key m (kbd "C-c RET") 'goto-address-at-point)
|
2000-03-20 13:12:14 +00:00
|
|
|
m)
|
|
|
|
"keymap to hold goto-addr's mouse key defs under highlighted URLs.")
|
|
|
|
|
|
|
|
(defcustom goto-address-url-face 'bold
|
2000-08-16 21:28:35 +00:00
|
|
|
"Face to use for URLs."
|
2000-03-20 13:12:14 +00:00
|
|
|
:type 'face
|
|
|
|
:group 'goto-address)
|
|
|
|
|
|
|
|
(defcustom goto-address-url-mouse-face 'highlight
|
2000-08-16 21:28:35 +00:00
|
|
|
"Face to use for URLs when the mouse is on them."
|
2000-03-20 13:12:14 +00:00
|
|
|
:type 'face
|
|
|
|
:group 'goto-address)
|
|
|
|
|
|
|
|
(defcustom goto-address-mail-face 'italic
|
2000-08-16 21:28:35 +00:00
|
|
|
"Face to use for e-mail addresses."
|
2000-03-20 13:12:14 +00:00
|
|
|
:type 'face
|
|
|
|
:group 'goto-address)
|
|
|
|
|
|
|
|
(defcustom goto-address-mail-mouse-face 'secondary-selection
|
2000-08-16 21:28:35 +00:00
|
|
|
"Face to use for e-mail addresses when the mouse is on them."
|
2000-03-20 13:12:14 +00:00
|
|
|
:type 'face
|
|
|
|
:group 'goto-address)
|
|
|
|
|
|
|
|
(defun goto-address-fontify ()
|
2000-06-15 11:43:01 +00:00
|
|
|
"Fontify the URLs and e-mail addresses in the current buffer.
|
2000-03-20 13:12:14 +00:00
|
|
|
This function implements `goto-address-highlight-p'
|
|
|
|
and `goto-address-fontify-p'."
|
2000-07-20 17:29:46 +00:00
|
|
|
;; Clean up from any previous go.
|
|
|
|
(dolist (overlay (overlays-in (point-min) (point-max)))
|
|
|
|
(if (overlay-get overlay 'goto-address)
|
|
|
|
(delete-overlay overlay)))
|
2000-03-20 13:12:14 +00:00
|
|
|
(save-excursion
|
2000-07-20 17:29:46 +00:00
|
|
|
(let ((inhibit-point-motion-hooks t))
|
2000-03-20 13:12:14 +00:00
|
|
|
(goto-char (point-min))
|
|
|
|
(if (< (- (point-max) (point)) goto-address-fontify-maximum-size)
|
|
|
|
(progn
|
|
|
|
(while (re-search-forward goto-address-url-regexp nil t)
|
|
|
|
(let* ((s (match-beginning 0))
|
|
|
|
(e (match-end 0))
|
|
|
|
(this-overlay (make-overlay s e)))
|
|
|
|
(and goto-address-fontify-p
|
|
|
|
(overlay-put this-overlay 'face goto-address-url-face))
|
|
|
|
(overlay-put this-overlay
|
|
|
|
'mouse-face goto-address-url-mouse-face)
|
2000-05-25 18:15:44 +00:00
|
|
|
(overlay-put this-overlay
|
|
|
|
'help-echo "mouse-2: follow URL")
|
2000-03-20 13:12:14 +00:00
|
|
|
(overlay-put this-overlay
|
2000-07-20 17:29:46 +00:00
|
|
|
'keymap goto-address-highlight-keymap)
|
|
|
|
(overlay-put this-overlay 'goto-address t)))
|
2000-03-20 13:12:14 +00:00
|
|
|
(goto-char (point-min))
|
|
|
|
(while (re-search-forward goto-address-mail-regexp nil t)
|
|
|
|
(let* ((s (match-beginning 0))
|
|
|
|
(e (match-end 0))
|
|
|
|
(this-overlay (make-overlay s e)))
|
|
|
|
(and goto-address-fontify-p
|
|
|
|
(overlay-put this-overlay 'face goto-address-mail-face))
|
|
|
|
(overlay-put this-overlay 'mouse-face
|
|
|
|
goto-address-mail-mouse-face)
|
2000-05-25 18:15:44 +00:00
|
|
|
(overlay-put this-overlay
|
2000-08-16 21:28:35 +00:00
|
|
|
'help-echo "mouse-2: mail this address")
|
2000-03-20 13:12:14 +00:00
|
|
|
(overlay-put this-overlay
|
2000-07-20 17:29:46 +00:00
|
|
|
'keymap goto-address-highlight-keymap)
|
|
|
|
(overlay-put this-overlay 'goto-address t))))))))
|
2000-03-20 13:12:14 +00:00
|
|
|
|
2001-10-05 09:36:02 +00:00
|
|
|
;; code to find and goto addresses; much of this has been blatantly
|
|
|
|
;; snarfed from browse-url.el
|
2000-03-20 13:12:14 +00:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun goto-address-at-mouse (event)
|
|
|
|
"Send to the e-mail address or load the URL clicked with the mouse.
|
|
|
|
Send mail to address at position of mouse click. See documentation for
|
|
|
|
`goto-address-find-address-at-point'. If no address is found
|
|
|
|
there, then load the URL at or before the position of the mouse click."
|
|
|
|
(interactive "e")
|
|
|
|
(save-excursion
|
2000-08-16 21:28:35 +00:00
|
|
|
(mouse-set-point event)
|
|
|
|
(goto-address-at-point)))
|
2000-03-20 13:12:14 +00:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun goto-address-at-point ()
|
|
|
|
"Send to the e-mail address or load the URL at point.
|
|
|
|
Send mail to address at point. See documentation for
|
|
|
|
`goto-address-find-address-at-point'. If no address is found
|
|
|
|
there, then load the URL at or before point."
|
|
|
|
(interactive)
|
|
|
|
(save-excursion
|
|
|
|
(let ((address (save-excursion (goto-address-find-address-at-point))))
|
2003-02-04 13:24:35 +00:00
|
|
|
(if (and address
|
2001-05-18 12:50:27 +00:00
|
|
|
(save-excursion
|
|
|
|
(goto-char (previous-single-char-property-change
|
|
|
|
(point) 'goto-address nil
|
|
|
|
(line-beginning-position)))
|
|
|
|
(not (looking-at goto-address-url-regexp))))
|
2000-08-16 21:28:35 +00:00
|
|
|
(compose-mail address)
|
|
|
|
(let ((url (browse-url-url-at-point)))
|
|
|
|
(if url
|
|
|
|
(browse-url url)
|
|
|
|
(error "No e-mail address or URL found")))))))
|
2000-03-20 13:12:14 +00:00
|
|
|
|
|
|
|
(defun goto-address-find-address-at-point ()
|
|
|
|
"Find e-mail address around or before point.
|
|
|
|
Then search backwards to beginning of line for the start of an e-mail
|
2000-08-16 21:28:35 +00:00
|
|
|
address. If no e-mail address found, return nil."
|
|
|
|
(re-search-backward "[^-_A-z0-9.@]" (line-beginning-position) 'lim)
|
|
|
|
(if (or (looking-at goto-address-mail-regexp) ; already at start
|
|
|
|
(and (re-search-forward goto-address-mail-regexp
|
|
|
|
(line-end-position) 'lim)
|
|
|
|
(goto-char (match-beginning 0))))
|
|
|
|
(match-string-no-properties 0)))
|
2000-03-20 13:12:14 +00:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun goto-address ()
|
|
|
|
"Sets up goto-address functionality in the current buffer.
|
|
|
|
Allows user to use mouse/keyboard command to click to go to a URL
|
|
|
|
or to send e-mail.
|
|
|
|
By default, goto-address binds to mouse-2 and C-c RET.
|
|
|
|
|
|
|
|
Also fontifies the buffer appropriately (see `goto-address-fontify-p' and
|
|
|
|
`goto-address-highlight-p' for more information)."
|
|
|
|
(interactive)
|
|
|
|
(if goto-address-highlight-p
|
|
|
|
(goto-address-fontify)))
|
|
|
|
|
|
|
|
(provide 'goto-addr)
|
|
|
|
|
2001-07-15 16:15:35 +00:00
|
|
|
;;; goto-addr.el ends here
|