Function: thing-at-point-url-at-point

thing-at-point-url-at-point is a byte-compiled function defined in thingatpt.el.gz.

Signature

(thing-at-point-url-at-point &optional LAX BOUNDS)

Documentation

Return the URL around or before point.

If no URL is found, return nil.

If optional argument LAX is non-nil, look for URLs that are not well-formed, such as foo@bar or <nobody>.

If optional arguments BOUNDS are non-nil, it should be a cons cell of the form (START . END), containing the beginning and end positions of the URI. Otherwise, these positions are detected automatically from the text around point.

If the scheme component is absent, either because a URI delimited with <url:...> lacks one, or because an ill-formed URI was found with LAX or BEG and END, try to add a scheme in the returned URI. The scheme is chosen heuristically: "mailto:" if the address looks like an email address, "ftp://" if it starts with
"ftp", etc.

Source Code

;; Defined in /usr/src/emacs/lisp/thingatpt.el.gz
(defun thing-at-point-url-at-point (&optional lax bounds)
  "Return the URL around or before point.
If no URL is found, return nil.

If optional argument LAX is non-nil, look for URLs that are not
well-formed, such as foo@bar or <nobody>.

If optional arguments BOUNDS are non-nil, it should be a cons
cell of the form (START . END), containing the beginning and end
positions of the URI.  Otherwise, these positions are detected
automatically from the text around point.

If the scheme component is absent, either because a URI delimited
with <url:...> lacks one, or because an ill-formed URI was found
with LAX or BEG and END, try to add a scheme in the returned URI.
The scheme is chosen heuristically: \"mailto:\" if the address
looks like an email address, \"ftp://\" if it starts with
\"ftp\", etc."
  (unless bounds
    (setq bounds (thing-at-point-bounds-of-url-at-point lax)))
  (when (and bounds (< (car bounds) (cdr bounds)))
    (let ((str (buffer-substring-no-properties (car bounds) (cdr bounds))))
      ;; If there is no scheme component, try to add one.
      (unless (string-match "\\`[a-zA-Z][-a-zA-Z0-9+.]*:" str)
	(or
	 ;; If the URI has the form <foo@bar>, treat it according to
	 ;; `thing-at-point-default-mail-uri-scheme'.  If there are
	 ;; no angle brackets, it must be mailto.
	 (when (string-match "\\`[^:</>@]+@[-.0-9=&?$+A-Z_a-z~#,%;*]" str)
	   (let ((scheme (if (and (eq (char-before (car bounds)) ?<)
				  (eq (char-after  (cdr bounds)) ?>))
			     thing-at-point-default-mail-uri-scheme
			   "mailto")))
	     (if scheme
		 (setq str (concat scheme ":" str)))))
	 ;; If the string is like <FOO>, where FOO is an existing user
	 ;; name on the system, treat that as an email address.
	 (and (string-match "\\`[[:alnum:]]+\\'" str)
	      (eq (char-before (car bounds)) ?<)
	      (eq (char-after  (cdr bounds)) ?>)
	      (not (string-search "~" (expand-file-name (concat "~" str))))
	      (setq str (concat "mailto:" str)))
	 ;; If it looks like news.example.com, treat it as news.
	 (if (thing-at-point-newsgroup-p str)
	     (setq str (concat "news:" str)))
	 ;; If it looks like ftp.example.com. treat it as ftp.
	 (if (string-match "\\`ftp\\." str)
	     (setq str (concat "ftp://" str)))
	 ;; If it looks like www.example.com. treat it as http.
	 (if (string-match "\\`www\\." str)
	     (setq str (concat "http://" str)))
	 ;; Otherwise, it just isn't a URI.
	 (setq str nil)))
      str)))