]> code.delx.au - gnu-emacs/blobdiff - lisp/net/goto-addr.el
Merge from emacs--rel--22
[gnu-emacs] / lisp / net / goto-addr.el
index 9ce73dba47bb43e17d493af6fa99dd144dd4cf12..e2556bdc6bbc59e00c09404eb99a6e2951998a70 100644 (file)
@@ -1,6 +1,7 @@
 ;;; goto-addr.el --- click to browse URL or to send to e-mail address
 
-;; Copyright (C) 1995, 2000, 2001, 2005  Free Software Foundation, Inc.
+;; Copyright (C) 1995, 2000, 2001, 2002, 2003, 2004,
+;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
 ;; Author: Eric Ding <ericding@alum.mit.edu>
 ;; Maintainer: FSF
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; 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.
+;; the Free Software Foundation, either version 3 of the License, 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
@@ -20,9 +21,7 @@
 ;; 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., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
@@ -104,12 +103,18 @@ A value of t means there is no limit--fontify regardless of the size."
   "A regular expression probably matching an e-mail address.")
 
 (defvar goto-address-url-regexp
-  (concat "\\<\\("
-         (mapconcat 'identity
-                    (delete "mailto:" (copy-sequence thing-at-point-uri-schemes))
-                    "\\|")
-         "\\)"
-          thing-at-point-url-path-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_=?#$@~`%&*+|\\/.,]*"
@@ -122,7 +127,7 @@ A value of t means there is no limit--fontify regardless of the size."
       'goto-address-at-point)
     (define-key m (kbd "C-c RET") 'goto-address-at-point)
     m)
-  "keymap to hold goto-addr's mouse key defs under highlighted URLs.")
+  "Keymap to hold goto-addr's mouse key defs under highlighted URLs.")
 
 (defcustom goto-address-url-face 'bold
   "Face to use for URLs."
@@ -144,49 +149,75 @@ A value of t means there is no limit--fontify regardless of the size."
   :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)
+
 (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'."
   ;; Clean up from any previous go.
-  (dolist (overlay (overlays-in (point-min) (point-max)))
-    (if (overlay-get overlay 'goto-address)
-       (delete-overlay overlay)))
+  (goto-address-unfontify (point-min) (point-max))
   (save-excursion
     (let ((inhibit-point-motion-hooks t))
       (goto-char (point-min))
-      (if (or (eq t goto-address-fontify-maximum-size)
-             (< (- (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 'evaporate t)
-               (overlay-put this-overlay
-                             'mouse-face goto-address-url-mouse-face)
-               (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 (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
-                            '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))))))))
+      (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)))))
 
 ;; code to find and goto addresses; much of this has been blatantly
 ;; snarfed from browse-url.el
@@ -203,7 +234,7 @@ Send mail to address at point.  See documentation for
 there, then load the URL at or before point."
   (interactive (list last-input-event))
   (save-excursion
-    (if event (mouse-set-point event))
+    (if event (posn-set-point (event-end event)))
     (let ((address (save-excursion (goto-address-find-address-at-point))))
       (if (and address
               (save-excursion
@@ -233,13 +264,41 @@ address.  If no e-mail address found, return nil."
   "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.
+By default, goto-address binds `goto-address-at-point' to mouse-2 and C-c RET
+only on URLs and e-mail addresses.
 
 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)
+
+;;;###autoload
+(define-minor-mode goto-address-mode
+  "Minor mode to buttonize URLs and e-mail addresses in the current buffer."
+  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
+  "Turn on `goto-address-mode', but only in 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)))))
 
 (provide 'goto-addr)