1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-12-15 09:47:20 +00:00
emacs/lisp/net/goto-addr.el

308 lines
11 KiB
EmacsLisp
Raw Normal View History

2000-03-20 13:12:14 +00:00
;;; goto-addr.el --- click to browse URL or to send to e-mail address
;; Copyright (C) 1995, 2000-2012 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
2000-03-20 13:12:14 +00:00
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
2000-03-20 13:12:14 +00:00
;; 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. If not, see <http://www.gnu.org/licenses/>.
2000-03-20 13:12:14 +00:00
;;; 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
Update docstrings and comments to use "init file" terminology. * bookmark.el (bookmark-bmenu-toggle-filenames): Doc fixes. * comint.el (comint-prompt-read-only): * custom.el (defcustom): * hi-lock.el (hi-lock-mode): * ibuffer.el (ibuffer-formats): * ielm.el (ielm-prompt-read-only): * novice.el (disable-command): * saveplace.el (toggle-save-place): * speedbar.el (speedbar-supported-extension-expressions): * startup.el (auto-save-list-file-prefix, init-file-user) (after-init-hook, inhibit-startup-echo-area-message): * strokes.el (strokes-help): * time-stamp.el (time-stamp): * calendar/calendar.el (calendar, diary-file): * calendar/diary-lib.el (diary-mail-entries, diary) (diary-list-entries-hook): * calendar/holidays.el (holidays, calendar-holidays): * calendar/lunar.el (lunar-phases): * calendar/solar.el (sunrise-sunset): * emulation/edt.el (edt-load-keys): * emulation/viper.el (viper-mode): * eshell/em-alias.el (eshell-command-aliases-list): * eshell/esh-util.el (eshell-convert-numeric-arguments): * international/ogonek.el (ogonek-information): * net/tramp-cmds.el (tramp-bug): * net/quickurl.el (quickurl-reread-hook-postfix): * play/decipher.el (decipher-font-lock-keywords): * progmodes/cc-styles.el (c-set-style): * progmodes/idlw-shell.el (idlwave-shell-prompt-pattern): * progmodes/inf-lisp.el (inferior-lisp-prompt): * progmodes/octave-mod.el (octave-mode): * progmodes/sql.el (sql-mode, sql-interactive-mode, sql-password): * progmodes/verilog-mode.el (verilog-read-defines): * textmodes/two-column.el (2C-mode): Likewise.
2012-09-17 05:41:04 +00:00
;; reading mail in mh-e), add this to your init file:
2000-03-20 13:12:14 +00:00
;;
;; (add-hook 'mh-show-mode-hook 'goto-address)
;;
;; 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-point)
2000-03-20 13:12:14 +00:00
;; 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)
;; * 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:
(require 'thingatpt)
(autoload 'browse-url-url-at-point "browse-url")
2000-03-20 13:12:14 +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 'comm)
2000-03-20 13:12:14 +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
"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
"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
"Maximum size of file in which to fontify and/or highlight URLs.
A value of t means there is no limit--fontify regardless of the size."
:type '(choice (integer :tag "Maximum size") (const :tag "No limit" t))
2000-03-20 13:12:14 +00:00
:group 'goto-address)
(defvar goto-address-mail-regexp
;; 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.")
(defvar goto-address-url-regexp
(concat
"\\<\\("
(mapconcat 'identity
(delete "mailto:"
;; Remove `data:', as it's not terribly useful to follow
;; those. Leaving them causes `use Data::Dumper;' to be
;; fontified oddly in Perl files.
(delete "data:"
(copy-sequence thing-at-point-uri-schemes)))
"\\|")
"\\)"
thing-at-point-url-path-regexp)
;; (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)))
(define-key m (if (featurep 'xemacs) (kbd "<button2>") (kbd "<mouse-2>"))
2005-05-06 21:25:39 +00:00
'goto-address-at-point)
(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.")
2000-03-20 13:12:14 +00:00
(defcustom goto-address-url-face 'link
"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
"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
"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
"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-unfontify (start end)
"Remove `goto-address' fontification from the given region."
(dolist (overlay (overlays-in start end))
(if (overlay-get overlay 'goto-address)
(delete-overlay overlay))))
(defvar goto-address-prog-mode)
2000-03-20 13:12:14 +00:00
(defun goto-address-fontify ()
"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'."
;; Clean up from any previous go.
(goto-address-unfontify (point-min) (point-max))
2000-03-20 13:12:14 +00:00
(save-excursion
(let ((inhibit-point-motion-hooks t))
2000-03-20 13:12:14 +00:00
(goto-char (point-min))
(when (or (eq t goto-address-fontify-maximum-size)
(< (- (point-max) (point)) goto-address-fontify-maximum-size))
(while (re-search-forward goto-address-url-regexp nil t)
(let* ((s (match-beginning 0))
(e (match-end 0))
this-overlay)
(when (or (not goto-address-prog-mode)
;; This tests for both comment and string
;; syntax.
(nth 8 (syntax-ppss)))
(setq this-overlay (make-overlay s e))
(and goto-address-fontify-p
(overlay-put this-overlay 'face goto-address-url-face))
(overlay-put this-overlay 'evaporate t)
(overlay-put this-overlay
'mouse-face goto-address-url-mouse-face)
(overlay-put this-overlay 'follow-link t)
(overlay-put this-overlay
'help-echo "mouse-2, C-c RET: 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)
(when (or (not goto-address-prog-mode)
;; This tests for both comment and string
;; syntax.
(nth 8 (syntax-ppss)))
(setq this-overlay (make-overlay s e))
(and goto-address-fontify-p
(overlay-put this-overlay 'face goto-address-mail-face))
(overlay-put this-overlay 'evaporate t)
(overlay-put this-overlay 'mouse-face
goto-address-mail-mouse-face)
(overlay-put this-overlay 'follow-link t)
(overlay-put this-overlay
'help-echo "mouse-2, C-c RET: mail this address")
(overlay-put this-overlay
'keymap goto-address-highlight-keymap)
(overlay-put this-overlay 'goto-address t))))))))
(defun goto-address-fontify-region (start end)
"Fontify URLs and e-mail addresses in the given region."
(save-excursion
(save-restriction
(let ((beg-line (progn (goto-char start) (line-beginning-position)))
(end-line (progn (goto-char end) (line-end-position))))
(narrow-to-region beg-line end-line)
(goto-address-fontify)))))
2000-03-20 13:12:14 +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
(define-obsolete-function-alias
'goto-address-at-mouse 'goto-address-at-point "22.1")
2000-03-20 13:12:14 +00:00
;;;###autoload
(defun goto-address-at-point (&optional event)
2000-03-20 13:12:14 +00:00
"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 (list last-input-event))
2000-03-20 13:12:14 +00:00
(save-excursion
(if event (posn-set-point (event-end event)))
2000-03-20 13:12:14 +00:00
(let ((address (save-excursion (goto-address-find-address-at-point))))
2003-02-04 13:24:35 +00:00
(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")))))))
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
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 `goto-address-at-point' to mouse-2 and C-c RET
only on URLs and e-mail addresses.
2000-03-20 13:12:14 +00:00
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)))
;;;###autoload(put 'goto-address 'safe-local-eval-function t)
2000-03-20 13:12:14 +00:00
;;;###autoload
(define-minor-mode goto-address-mode
"Minor mode to buttonize URLs and e-mail addresses in the current buffer.
With a prefix argument ARG, enable the mode if ARG is positive,
and disable it otherwise. If called from Lisp, enable the mode
if ARG is omitted or nil."
nil
""
nil
(if goto-address-mode
(jit-lock-register #'goto-address-fontify-region)
(jit-lock-unregister #'goto-address-fontify-region)
(save-restriction
(widen)
(goto-address-unfontify (point-min) (point-max)))))
;;;###autoload
(define-minor-mode goto-address-prog-mode
Fix more minor mode docstrings. * lisp/emulation/cua-base.el (cua-mode): * lisp/mail/footnote.el (footnote-mode): * lisp/mail/mailabbrev.el (mail-abbrevs-mode): * lisp/net/xesam.el (xesam-minor-mode): * lisp/progmodes/bug-reference.el (bug-reference-mode): * lisp/progmodes/cap-words.el (capitalized-words-mode): * lisp/progmodes/compile.el (compilation-minor-mode) (compilation-shell-minor-mode): * lisp/progmodes/gud.el (gud-tooltip-mode): * lisp/progmodes/hideif.el (hide-ifdef-mode): * lisp/progmodes/idlw-shell.el (idlwave-shell-electric-debug-mode): * lisp/progmodes/subword.el (subword-mode): * lisp/progmodes/vhdl-mode.el (vhdl-electric-mode, vhdl-stutter-mode): * lisp/progmodes/which-func.el (which-function-mode): * lisp/term/tvi970.el (tvi970-set-keypad-mode): * lisp/term/vt100.el (vt100-wide-mode): * lisp/textmodes/flyspell.el (flyspell-mode): * lisp/textmodes/ispell.el (ispell-minor-mode): * lisp/textmodes/nroff-mode.el (nroff-electric-mode): * lisp/textmodes/paragraphs.el (use-hard-newlines): * lisp/textmodes/refill.el (refill-mode): * lisp/textmodes/reftex.el (reftex-mode): * lisp/textmodes/rst.el (rst-minor-mode): * lisp/textmodes/sgml-mode.el (html-autoview-mode) (sgml-electric-tag-pair-mode): * lisp/textmodes/tex-mode.el (latex-electric-env-pair-mode): * lisp/vc/diff-mode.el (diff-auto-refine-mode, diff-minor-mode): * lisp/emulation/crisp.el (crisp-mode): * lisp/emacs-lisp/eldoc.el (eldoc-mode): * lisp/emacs-lisp/checkdoc.el (checkdoc-minor-mode): Doc fixes for new minor mode behavior. * lisp/erc/erc-fill.el (erc-fill-mode): * lisp/erc/erc-track.el (erc-track-minor-mode): Doc fix. * lisp/erc/erc.el (define-erc-module): Fix autogenerated docstring to reflect Emacs 24 minor mode changes. * lisp/gnus/gnus-cite.el (gnus-message-citation-mode): Doc fix (in Emacs 24, calling a minor mode from Lisp with nil arg enables it, so we have to make the working a bit ambiguous here).
2011-10-20 00:26:14 +00:00
"Like `goto-address-mode', but only for comments and strings."
nil
""
nil
(if goto-address-prog-mode
(jit-lock-register #'goto-address-fontify-region)
(jit-lock-unregister #'goto-address-fontify-region)
(save-restriction
(widen)
(goto-address-unfontify (point-min) (point-max)))))
2000-03-20 13:12:14 +00:00
(provide 'goto-addr)
;;; goto-addr.el ends here