*** empty log message ***
[bpt/emacs.git] / lisp / thingatpt.el
index c76a3bf..ce3a3a5 100644 (file)
@@ -1,7 +1,7 @@
 ;;; thingatpt.el --- get the `thing' at point
 
-;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000
-;;   2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
+;;   2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
 ;; Author: Mike Williams <mikew@gopher.dosli.govt.nz>
 ;; Maintainer: FSF
@@ -12,7 +12,7 @@
 
 ;; 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)
+;; the Free Software Foundation; either version 3, or (at your option)
 ;; any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
@@ -53,7 +53,7 @@
 
 ;;;###autoload
 (defun forward-thing (thing &optional n)
-  "Move forward to the end of the next THING."
+  "Move forward to the end of the Nth next THING."
   (let ((forward-op (or (get thing 'forward-op)
                        (intern-soft (format "forward-%s" thing)))))
     (if (functionp forward-op)
@@ -67,7 +67,7 @@
   "Determine the start and end buffer locations for the THING at point.
 THING is a symbol which specifies the kind of syntactic entity you want.
 Possibilities include `symbol', `list', `sexp', `defun', `filename', `url',
-`word', `sentence', `whitespace', `line', `page' and others.
+`email', `word', `sentence', `whitespace', `line', `page' and others.
 
 See the file `thingatpt.el' for documentation on how to define
 a symbol as a valid THING.
@@ -80,16 +80,13 @@ of the textual entity that was found."
       (condition-case nil
          (save-excursion
            ;; Try moving forward, then back.
-           (let ((end (progn
-                        (funcall
-                         (or (get thing 'end-op)
-                             (function (lambda () (forward-thing thing 1)))))
-                        (point)))
-                 (beg (progn
-                        (funcall
-                         (or (get thing 'beginning-op)
-                             (function (lambda () (forward-thing thing -1)))))
-                        (point))))
+            (funcall ;; First move to end.
+             (or (get thing 'end-op)
+                 (lambda () (forward-thing thing 1))))
+            (funcall ;; Then move to beg.
+             (or (get thing 'beginning-op)
+                 (lambda () (forward-thing thing -1))))
+           (let ((beg (point)))
              (if (not (and beg (> beg orig)))
                  ;; If that brings us all the way back to ORIG,
                  ;; it worked.  But END may not be the real end.
@@ -98,28 +95,25 @@ of the textual entity that was found."
                         (progn
                           (funcall
                            (or (get thing 'end-op)
-                               (function (lambda () (forward-thing thing 1)))))
+                                (lambda () (forward-thing thing 1))))
                           (point))))
                    (if (and beg real-end (<= beg orig) (<= orig real-end))
                        (cons beg real-end)))
                (goto-char orig)
                ;; Try a second time, moving backward first and then forward,
                ;; so that we can find a thing that ends at ORIG.
-               (let ((beg (progn
-                            (funcall
-                             (or (get thing 'beginning-op)
-                                 (function (lambda () (forward-thing thing -1)))))
-                            (point)))
-                     (end (progn
-                            (funcall
-                             (or (get thing 'end-op)
-                                 (function (lambda () (forward-thing thing 1)))))
-                            (point)))
-                     (real-beg
+                (funcall ;; First, move to beg.
+                 (or (get thing 'beginning-op)
+                     (lambda () (forward-thing thing -1))))
+                (funcall ;; Then move to end.
+                 (or (get thing 'end-op)
+                     (lambda () (forward-thing thing 1))))
+               (let ((end (point))
+                      (real-beg
                       (progn
                         (funcall
                          (or (get thing 'beginning-op)
-                             (function (lambda () (forward-thing thing -1)))))
+                              (lambda () (forward-thing thing -1))))
                         (point))))
                  (if (and real-beg end (<= real-beg orig) (<= orig end))
                      (cons real-beg end))))))
@@ -130,7 +124,7 @@ of the textual entity that was found."
   "Return the THING at point.
 THING is a symbol which specifies the kind of syntactic entity you want.
 Possibilities include `symbol', `list', `sexp', `defun', `filename', `url',
-`word', `sentence', `whitespace', `line', `page' and others.
+`email', `word', `sentence', `whitespace', `line', `page' and others.
 
 See the file `thingatpt.el' for documentation on how to define
 a symbol as a valid THING."
@@ -160,7 +154,7 @@ a symbol as a valid THING."
 ;; and it has no final newline.
 
 (put 'line 'beginning-op
-     (function (lambda () (if (bolp) (forward-line -1) (beginning-of-line)))))
+     (lambda () (if (bolp) (forward-line -1) (beginning-of-line))))
 
 ;;  Sexps
 
@@ -190,7 +184,7 @@ a symbol as a valid THING."
 
 ;;  Lists
 
-(put 'list 'end-op (function (lambda () (up-list 1))))
+(put 'list 'end-op (lambda () (up-list 1)))
 (put 'list 'beginning-op 'backward-sexp)
 
 ;;  Filenames and URLs  www.com/foo%32bar
@@ -220,16 +214,19 @@ Hostname matching is stricter in this case than for
 ``thing-at-point-url-regexp''.")
 
 (defvar thing-at-point-uri-schemes
-  ;; Officials from http://www.iana.org/assignments/uri-schemes
+  ;; Officials from http://www.iana.org/assignments/uri-schemes.html
   '("ftp://" "http://" "gopher://" "mailto:" "news:" "nntp:"
     "telnet://" "wais://" "file:/" "prospero:" "z39.50s:" "z39.50r:"
     "cid:" "mid:" "vemmi:" "service:" "imap:" "nfs:" "acap:" "rtsp:"
     "tip:" "pop:" "data:" "dav:" "opaquelocktoken:" "sip:" "tel:" "fax:"
     "modem:" "ldap:" "https://" "soap.beep:" "soap.beeps:" "urn:" "go:"
     "afs:" "tn3270:" "mailserver:"
+    "crid:" "dict:" "dns:" "dtn:" "h323:" "im:" "info:" "ipp:"
+    "iris.beep:" "mtqp:" "mupdate:" "pres:" "sips:" "snmp:" "tag:"
+    "tftp:" "xmlrpc.beep:" "xmlrpc.beeps:" "xmpp:"
   ;; Compatibility
-    "snews:")
-  "Uniform Resource Identifier (URI) Schemes")
+    "snews:" "irc:" "mms://" "mmsh://")
+  "Uniform Resource Identifier (URI) Schemes.")
 
 (defvar thing-at-point-url-regexp
   (concat "\\<\\(" (mapconcat 'identity thing-at-point-uri-schemes "\\|") "\\)"
@@ -243,18 +240,19 @@ This may contain whitespace (including newlines) .")
 
 (put 'url 'bounds-of-thing-at-point 'thing-at-point-bounds-of-url-at-point)
 (defun thing-at-point-bounds-of-url-at-point ()
-  (let ((url "") short strip)
-    (if (or (setq strip (thing-at-point-looking-at
-                        thing-at-point-markedup-url-regexp))
+  (let ((strip (thing-at-point-looking-at
+                        thing-at-point-markedup-url-regexp))) ;; (url "") short
+    (if (or strip
            (thing-at-point-looking-at thing-at-point-url-regexp)
            ;; Access scheme omitted?
-           (setq short (thing-at-point-looking-at
-                        thing-at-point-short-url-regexp)))
+           ;; (setq short (thing-at-point-looking-at
+           ;;           thing-at-point-short-url-regexp))
+            )
        (let ((beginning (match-beginning 0))
              (end (match-end 0)))
-         (cond (strip
-                (setq beginning (+ beginning 5))
-                (setq end (- end 1))))
+         (when strip
+            (setq beginning (+ beginning 5))
+            (setq end (- end 1)))
          (cons beginning end)))))
 
 (put 'url 'thing-at-point 'thing-at-point-url-at-point)
@@ -280,7 +278,10 @@ starts with \"ftp\" and not \"ftp:/\", or \"http://\" by default."
          ;; strip whitespace
          (while (string-match "[ \t\n\r]+" url)
            (setq url (replace-match "" t t url)))
-         (and short (setq url (concat (cond ((string-match "@" url)
+         (and short (setq url (concat (cond ((string-match "^[a-zA-Z]+:" url)
+                                              ;; already has a URL scheme.
+                                              "")
+                                            ((string-match "@" url)
                                               "mailto:")
                                             ;; e.g. ftp.swiss... or ftp-swiss...
                                              ((string-match "^ftp" url)
@@ -327,17 +328,44 @@ point."
        (looking-at regexp)))))
 
 (put 'url 'end-op
-     (function (lambda ()
-                (let ((bounds (thing-at-point-bounds-of-url-at-point)))
-                  (if bounds
-                      (goto-char (cdr bounds))
-                    (error "No URL here"))))))
+     (lambda ()
+       (let ((bounds (thing-at-point-bounds-of-url-at-point)))
+         (if bounds
+             (goto-char (cdr bounds))
+           (error "No URL here")))))
 (put 'url 'beginning-op
-     (function (lambda ()
-                (let ((bounds (thing-at-point-bounds-of-url-at-point)))
-                  (if bounds
-                      (goto-char (car bounds))
-                    (error "No URL here"))))))
+     (lambda ()
+       (let ((bounds (thing-at-point-bounds-of-url-at-point)))
+         (if bounds
+             (goto-char (car bounds))
+           (error "No URL here")))))
+
+;;   Email addresses
+(defvar thing-at-point-email-regexp
+  "<?[-+_.~a-zA-Z][-+_.~:a-zA-Z0-9]*@[-.a-zA-Z0-9]+>?"
+  "A regular expression probably matching an email address.
+This does not match the real name portion, only the address, optionally
+with angle brackets.")
+
+;; Haven't set 'forward-op on 'email nor defined 'forward-email' because
+;; not sure they're actually needed, and URL seems to skip them too.
+;; Note that (end-of-thing 'email) and (beginning-of-thing 'email)
+;; work automagically, though.
+
+(put 'email 'bounds-of-thing-at-point
+     (lambda ()
+       (let ((thing (thing-at-point-looking-at thing-at-point-email-regexp)))
+         (if thing
+             (let ((beginning (match-beginning 0))
+                   (end (match-end 0)))
+               (cons beginning end))))))
+
+(put 'email 'thing-at-point
+     (lambda ()
+       (let ((boundary-pair (bounds-of-thing-at-point 'email)))
+         (if boundary-pair
+             (buffer-substring-no-properties
+              (car boundary-pair) (cdr boundary-pair))))))
 
 ;;  Whitespace
 
@@ -385,7 +413,7 @@ point."
 (defun sentence-at-point () (thing-at-point 'sentence))
 
 (defun read-from-whole-string (str)
-  "Read a lisp expression from STR.
+  "Read a Lisp expression from STR.
 Signal an error if the entire string was not used."
   (let* ((read-data (read-from-string str))
         (more-left
@@ -407,11 +435,13 @@ Signal an error if the entire string was not used."
 ;;;###autoload
 (defun sexp-at-point ()   (form-at-point 'sexp))
 ;;;###autoload
-(defun symbol-at-point () (form-at-point 'sexp 'symbolp))
+(defun symbol-at-point ()
+  (let ((thing (thing-at-point 'symbol)))
+    (if thing (intern thing))))
 ;;;###autoload
 (defun number-at-point () (form-at-point 'sexp 'numberp))
 ;;;###autoload
 (defun list-at-point ()   (form-at-point 'list 'listp))
 
-;;; arch-tag: bb65a163-dae2-4055-aedc-fe11f497f698
+;; arch-tag: bb65a163-dae2-4055-aedc-fe11f497f698
 ;;; thingatpt.el ends here