goto-addr.el 11 KB

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