(require 'thingatpt)
(autoload 'browse-url-url-at-point "browse-url")
(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))
(defgroup goto-address nil
"Click to browse URL or to send to e-mail address."
:group 'mouse
:group 'hypermedia)
(defcustom goto-address-fontify-p t
"*Non-nil means URLs and e-mail addresses in buffer are fontified.
But only if `goto-address-highlight-p' is also non-nil."
:type 'boolean
:group 'goto-address)
(defcustom goto-address-highlight-p t
"*Non-nil means URLs and e-mail addresses in buffer are highlighted."
:type 'boolean
:group 'goto-address)
(defcustom goto-address-fontify-maximum-size 30000
"*Maximum size of file in which to fontify and/or highlight URLs."
:type 'integer
:group 'goto-address)
(defvar goto-address-mail-regexp
"[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+"
"A regular expression probably matching an e-mail address.")
(defvar goto-address-url-regexp thing-at-point-url-regexp
"A regular expression probably matching a URL.")
(defvar goto-address-highlight-keymap
(let ((m (make-sparse-keymap)))
(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)
m)
"keymap to hold goto-addr's mouse key defs under highlighted URLs.")
(defcustom goto-address-url-face 'bold
"Face to use for URLs."
:type 'face
:group 'goto-address)
(defcustom goto-address-url-mouse-face 'highlight
"Face to use for URLs when the mouse is on them."
:type 'face
:group 'goto-address)
(defcustom goto-address-mail-face 'italic
"Face to use for e-mail addresses."
:type 'face
:group 'goto-address)
(defcustom goto-address-mail-mouse-face 'secondary-selection
"Face to use for e-mail addresses when the mouse is on them."
:type 'face
:group 'goto-address)
(defun goto-address-fontify ()
"Fontify the URLs and e-mail addresses in the current buffer.
This function implements `goto-address-highlight-p'
and `goto-address-fontify-p'."
(dolist (overlay (overlays-in (point-min) (point-max)))
(if (overlay-get overlay 'goto-address)
(delete-overlay overlay)))
(save-excursion
(let ((inhibit-point-motion-hooks t))
(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)
(overlay-put this-overlay
'help-echo "mouse-2: follow URL")
(overlay-put this-overlay
'keymap goto-address-highlight-keymap)
(overlay-put this-overlay 'goto-address t)))
(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)
(overlay-put this-overlay
'help-echo "mouse-2: mail this address")
(overlay-put this-overlay
'keymap goto-address-highlight-keymap)
(overlay-put this-overlay 'goto-address t))))))))
(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
(mouse-set-point event)
(goto-address-at-point)))
(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))))
(if (and address
(save-excursion
(goto-char (previous-single-char-property-change
(point) 'goto-address nil
(line-beginning-position)))
(not (looking-at goto-address-url-regexp))))
(compose-mail address)
(let ((url (browse-url-url-at-point)))
(if url
(browse-url url)
(error "No e-mail address or URL found")))))))
(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
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) (and (re-search-forward goto-address-mail-regexp
(line-end-position) 'lim)
(goto-char (match-beginning 0))))
(match-string-no-properties 0)))
(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)