Rewrite Eshell tests using ERT
[bpt/emacs.git] / lisp / thingatpt.el
CommitLineData
55535639 1;;; thingatpt.el --- get the `thing' at point
1a2b6c52 2
ab422c4d 3;; Copyright (C) 1991-1998, 2000-2013 Free Software Foundation, Inc.
1a2b6c52
RS
4
5;; Author: Mike Williams <mikew@gopher.dosli.govt.nz>
6254fc9f 6;; Maintainer: FSF
b7f66977 7;; Keywords: extensions, matching, mouse
1a2b6c52 8;; Created: Thu Mar 28 13:48:23 1991
1a2b6c52
RS
9
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
1a2b6c52 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
1a2b6c52
RS
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
eb3fa2cf
GM
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
69f9ba7e 25;;; Commentary:
b578f267 26
c851323f
RS
27;; This file provides routines for getting the "thing" at the location of
28;; point, whatever that "thing" happens to be. The "thing" is defined by
7a8f27db 29;; its beginning and end positions in the buffer.
1a2b6c52
RS
30;;
31;; The function bounds-of-thing-at-point finds the beginning and end
c851323f 32;; positions by moving first forward to the end of the "thing", and then
1a2b6c52 33;; backwards to the beginning. By default, it uses the corresponding
c851323f 34;; forward-"thing" operator (eg. forward-word, forward-line).
1a2b6c52
RS
35;;
36;; Special cases are allowed for using properties associated with the named
f1180544 37;; "thing":
1a2b6c52 38;;
c851323f 39;; forward-op Function to call to skip forward over a "thing" (or
1a2b6c52 40;; with a negative argument, backward).
f1180544 41;;
c851323f
RS
42;; beginning-op Function to call to skip to the beginning of a "thing".
43;; end-op Function to call to skip to the end of a "thing".
1a2b6c52
RS
44;;
45;; Reliance on existing operators means that many `things' can be accessed
46;; without further code: eg.
47;; (thing-at-point 'line)
48;; (thing-at-point 'page)
49
b578f267 50;;; Code:
1a2b6c52
RS
51
52(provide 'thingatpt)
53
b578f267 54;; Basic movement
1a2b6c52
RS
55
56;;;###autoload
c851323f 57(defun forward-thing (thing &optional n)
f5bd0689
CY
58 "Move forward to the end of the Nth next THING.
59THING should be a symbol specifying a type of syntactic entity.
60Possibilities include `symbol', `list', `sexp', `defun',
61`filename', `url', `email', `word', `sentence', `whitespace',
62`line', and `page'."
c851323f
RS
63 (let ((forward-op (or (get thing 'forward-op)
64 (intern-soft (format "forward-%s" thing)))))
6254fc9f 65 (if (functionp forward-op)
c851323f
RS
66 (funcall forward-op (or n 1))
67 (error "Can't determine how to move over a %s" thing))))
1a2b6c52 68
b578f267 69;; General routines
1a2b6c52
RS
70
71;;;###autoload
c851323f
RS
72(defun bounds-of-thing-at-point (thing)
73 "Determine the start and end buffer locations for the THING at point.
f5bd0689
CY
74THING should be a symbol specifying a type of syntactic entity.
75Possibilities include `symbol', `list', `sexp', `defun',
76`filename', `url', `email', `word', `sentence', `whitespace',
77`line', and `page'.
c851323f 78
f5bd0689
CY
79See the file `thingatpt.el' for documentation on how to define a
80valid THING.
c851323f 81
f5bd0689
CY
82Return a cons cell (START . END) giving the start and end
83positions of the thing found."
d9cc804b
RS
84 (if (get thing 'bounds-of-thing-at-point)
85 (funcall (get thing 'bounds-of-thing-at-point))
86 (let ((orig (point)))
87 (condition-case nil
88 (save-excursion
89 ;; Try moving forward, then back.
2a59b30d
SM
90 (funcall ;; First move to end.
91 (or (get thing 'end-op)
92 (lambda () (forward-thing thing 1))))
93 (funcall ;; Then move to beg.
94 (or (get thing 'beginning-op)
95 (lambda () (forward-thing thing -1))))
96 (let ((beg (point)))
f278f87f 97 (if (<= beg orig)
d9cc804b
RS
98 ;; If that brings us all the way back to ORIG,
99 ;; it worked. But END may not be the real end.
100 ;; So find the real end that corresponds to BEG.
f278f87f 101 ;; FIXME: in which cases can `real-end' differ from `end'?
d9cc804b 102 (let ((real-end
f1180544
JB
103 (progn
104 (funcall
105 (or (get thing 'end-op)
2a59b30d 106 (lambda () (forward-thing thing 1))))
d9cc804b 107 (point))))
f278f87f
SM
108 (when (and (<= orig real-end) (< beg real-end))
109 (cons beg real-end)))
d9cc804b
RS
110 (goto-char orig)
111 ;; Try a second time, moving backward first and then forward,
112 ;; so that we can find a thing that ends at ORIG.
2a59b30d
SM
113 (funcall ;; First, move to beg.
114 (or (get thing 'beginning-op)
115 (lambda () (forward-thing thing -1))))
116 (funcall ;; Then move to end.
117 (or (get thing 'end-op)
118 (lambda () (forward-thing thing 1))))
119 (let ((end (point))
120 (real-beg
f1180544
JB
121 (progn
122 (funcall
123 (or (get thing 'beginning-op)
2a59b30d 124 (lambda () (forward-thing thing -1))))
d9cc804b 125 (point))))
f278f87f 126 (if (and (<= real-beg orig) (<= orig end) (< real-beg end))
d9cc804b
RS
127 (cons real-beg end))))))
128 (error nil)))))
1a2b6c52
RS
129
130;;;###autoload
72d3cfca 131(defun thing-at-point (thing &optional no-properties)
c851323f 132 "Return the THING at point.
f5bd0689
CY
133THING should be a symbol specifying a type of syntactic entity.
134Possibilities include `symbol', `list', `sexp', `defun',
135`filename', `url', `email', `word', `sentence', `whitespace',
e84cad57 136`line', `number', and `page'.
c851323f 137
72d3cfca
SS
138When the optional argument NO-PROPERTIES is non-nil,
139strip text properties from the return value.
140
c851323f
RS
141See the file `thingatpt.el' for documentation on how to define
142a symbol as a valid THING."
72d3cfca
SS
143 (let ((text
144 (if (get thing 'thing-at-point)
145 (funcall (get thing 'thing-at-point))
146 (let ((bounds (bounds-of-thing-at-point thing)))
147 (when bounds
148 (buffer-substring (car bounds) (cdr bounds)))))))
149 (when (and text no-properties)
150 (set-text-properties 0 (length text) nil text))
151 text))
1a2b6c52 152
b578f267 153;; Go to beginning/end
1a2b6c52 154
c851323f 155(defun beginning-of-thing (thing)
f5bd0689
CY
156 "Move point to the beginning of THING.
157The bounds of THING are determined by `bounds-of-thing-at-point'."
c851323f
RS
158 (let ((bounds (bounds-of-thing-at-point thing)))
159 (or bounds (error "No %s here" thing))
1a2b6c52
RS
160 (goto-char (car bounds))))
161
c851323f 162(defun end-of-thing (thing)
f5bd0689
CY
163 "Move point to the end of THING.
164The bounds of THING are determined by `bounds-of-thing-at-point'."
c851323f
RS
165 (let ((bounds (bounds-of-thing-at-point thing)))
166 (or bounds (error "No %s here" thing))
1a2b6c52
RS
167 (goto-char (cdr bounds))))
168
f1180544 169;; Special cases
1a2b6c52 170
f1180544 171;; Lines
9f5c7ace
RS
172
173;; bolp will be false when you click on the last line in the buffer
174;; and it has no final newline.
175
176(put 'line 'beginning-op
2a59b30d 177 (lambda () (if (bolp) (forward-line -1) (beginning-of-line))))
9f5c7ace 178
f1180544 179;; Sexps
1a2b6c52
RS
180
181(defun in-string-p ()
f5bd0689
CY
182 "Return non-nil if point is in a string.
183\[This is an internal function.]"
1a2b6c52
RS
184 (let ((orig (point)))
185 (save-excursion
186 (beginning-of-defun)
187 (nth 3 (parse-partial-sexp (point) orig)))))
188
189(defun end-of-sexp ()
f5bd0689
CY
190 "Move point to the end of the current sexp.
191\[This is an internal function.]"
95b43468 192 (let ((char-syntax (syntax-after (point))))
1a2b6c52
RS
193 (if (or (eq char-syntax ?\))
194 (and (eq char-syntax ?\") (in-string-p)))
195 (forward-char 1)
196 (forward-sexp 1))))
197
198(put 'sexp 'end-op 'end-of-sexp)
199
6f0e09d4 200(defun beginning-of-sexp ()
f5bd0689
CY
201 "Move point to the beginning of the current sexp.
202\[This is an internal function.]"
ce8fb8aa 203 (let ((char-syntax (char-syntax (char-before))))
6f0e09d4
RS
204 (if (or (eq char-syntax ?\()
205 (and (eq char-syntax ?\") (in-string-p)))
206 (forward-char -1)
207 (forward-sexp -1))))
208
209(put 'sexp 'beginning-op 'beginning-of-sexp)
210
f1180544 211;; Lists
1a2b6c52 212
965b9376
CY
213(put 'list 'bounds-of-thing-at-point 'thing-at-point-bounds-of-list-at-point)
214
215(defun thing-at-point-bounds-of-list-at-point ()
f5bd0689
CY
216 "Return the bounds of the list at point.
217\[Internal function used by `bounds-of-thing-at-point'.]"
965b9376
CY
218 (save-excursion
219 (let ((opoint (point))
220 (beg (condition-case nil
221 (progn (up-list -1)
222 (point))
223 (error nil))))
224 (condition-case nil
225 (if beg
226 (progn (forward-sexp)
227 (cons beg (point)))
228 ;; Are we are at the beginning of a top-level sexp?
229 (forward-sexp)
230 (let ((end (point)))
231 (backward-sexp)
232 (if (>= opoint (point))
233 (cons opoint end))))
234 (error nil)))))
1a2b6c52 235
e8974c48
DA
236;; Defuns
237
238(put 'defun 'beginning-op 'beginning-of-defun)
239(put 'defun 'end-op 'end-of-defun)
240(put 'defun 'forward-op 'end-of-defun)
241
6e5c1569 242;; Filenames
1a2b6c52 243
839aacc9 244(defvar thing-at-point-file-name-chars "-~/[:alnum:]_.${}#%,:"
1a2b6c52
RS
245 "Characters allowable in filenames.")
246
f1180544 247(put 'filename 'end-op
839aacc9
DL
248 (lambda ()
249 (re-search-forward (concat "\\=[" thing-at-point-file-name-chars "]*")
250 nil t)))
1a2b6c52 251(put 'filename 'beginning-op
839aacc9
DL
252 (lambda ()
253 (if (re-search-backward (concat "[^" thing-at-point-file-name-chars "]")
254 nil t)
255 (forward-char)
256 (goto-char (point-min)))))
c851323f 257
6e5c1569
CY
258;; URIs
259
260(defvar thing-at-point-beginning-of-url-regexp nil
261 "Regexp matching the beginning of a well-formed URI.
262If nil, construct the regexp from `thing-at-point-uri-schemes'.")
263
d9cc804b 264(defvar thing-at-point-url-path-regexp
42986283 265 "[^]\t\n \"'<>[^`{}]*[^]\t\n \"'<>[^`{}.,;]+"
6e5c1569 266 "Regexp matching the host and filename or e-mail part of a URL.")
d9cc804b
RS
267
268(defvar thing-at-point-short-url-regexp
d61bdd5d 269 (concat "[-A-Za-z0-9]+\\.[-A-Za-z0-9.]+" thing-at-point-url-path-regexp)
6e5c1569 270 "Regexp matching a URI without a scheme component.")
d9cc804b 271
1c1766c7 272(defvar thing-at-point-uri-schemes
4f5d4668 273 ;; Officials from http://www.iana.org/assignments/uri-schemes.html
6e5c1569
CY
274 '("aaa://" "about:" "acap://" "apt:" "bzr://" "bzr+ssh://"
275 "attachment:/" "chrome://" "cid:" "content://" "crid://" "cvs://"
276 "data:" "dav:" "dict://" "doi:" "dns:" "dtn:" "feed:" "file:/"
277 "finger://" "fish://" "ftp://" "geo:" "git://" "go:" "gopher://"
278 "h323:" "http://" "https://" "im:" "imap://" "info:" "ipp:"
279 "irc://" "irc6://" "ircs://" "iris.beep:" "jar:" "ldap://"
280 "ldaps://" "mailto:" "mid:" "mtqp://" "mupdate://" "news:"
281 "nfs://" "nntp://" "opaquelocktoken:" "pop://" "pres:"
282 "resource://" "rmi://" "rsync://" "rtsp://" "rtspu://" "service:"
283 "sftp://" "sip:" "sips:" "smb://" "sms:" "snmp://" "soap.beep://"
284 "soap.beeps://" "ssh://" "svn://" "svn+ssh://" "tag:" "tel:"
285 "telnet://" "tftp://" "tip://" "tn3270://" "udp://" "urn:"
286 "uuid:" "vemmi://" "webcal://" "xri://" "xmlrpc.beep://"
287 "xmlrpc.beeps://" "z39.50r://" "z39.50s://" "xmpp:"
288 ;; Compatibility
289 "fax:" "mms://" "mmsh://" "modem:" "prospero:" "snews:"
290 "wais://")
291 "List of URI schemes recognized by `thing-at-point-url-at-point'.
292Each string in this list should correspond to the start of a
293URI's scheme component, up to and including the trailing // if
294the scheme calls for that to be present.")
295
296(defvar thing-at-point-markedup-url-regexp "<URL:\\([^<>\n]+\\)>"
297 "Regexp matching a URL marked up per RFC1738.
298This kind of markup was formerly recommended as a way to indicate
299URIs, but as of RFC 3986 it is no longer recommended.
300Subexpression 1 should contain the delimited URL.")
301
302(defvar thing-at-point-newsgroup-regexp
303 "\\`[[:lower:]]+\\.[-+[:lower:]_0-9.]+\\'"
304 "Regexp matching a newsgroup name.")
305
306(defvar thing-at-point-newsgroup-heads
307 '("alt" "comp" "gnu" "misc" "news" "sci" "soc" "talk")
308 "Used by `thing-at-point-newsgroup-p' if gnus is not running.")
309
310(defvar thing-at-point-default-mail-uri-scheme "mailto"
311 "Default scheme for ill-formed URIs that look like <foo@example.com>.
312If nil, do not give such URIs a scheme.")
d9cc804b
RS
313
314(put 'url 'bounds-of-thing-at-point 'thing-at-point-bounds-of-url-at-point)
6e5c1569
CY
315
316(defun thing-at-point-bounds-of-url-at-point (&optional lax)
317 "Return a cons cell containing the start and end of the URI at point.
318Try to find a URI using `thing-at-point-markedup-url-regexp'.
319If that fails, try with `thing-at-point-beginning-of-url-regexp'.
320If that also fails, and optional argument LAX is non-nil, return
321the bounds of a possible ill-formed URI (one lacking a scheme)."
322 ;; Look for the old <URL:foo> markup. If found, use it.
323 (or (thing-at-point--bounds-of-markedup-url)
324 ;; Otherwise, find the bounds within which a URI may exist. The
325 ;; method is similar to `ffap-string-at-point'. Note that URIs
326 ;; may contain parentheses but may not contain spaces (RFC3986).
327 (let* ((allowed-chars "--:=&?$+@-Z_[:alpha:]~#,%;*()!'")
328 (skip-before "^[0-9a-zA-Z]")
329 (skip-after ":;.,!?")
330 (pt (point))
331 (beg (save-excursion
332 (skip-chars-backward allowed-chars)
333 (skip-chars-forward skip-before pt)
334 (point)))
335 (end (save-excursion
336 (skip-chars-forward allowed-chars)
337 (skip-chars-backward skip-after pt)
338 (point))))
339 (or (thing-at-point--bounds-of-well-formed-url beg end pt)
340 (if lax (cons beg end))))))
341
342(defun thing-at-point--bounds-of-markedup-url ()
343 (when thing-at-point-markedup-url-regexp
344 (let ((case-fold-search t)
345 (pt (point))
346 (beg (line-beginning-position))
347 (end (line-end-position))
348 found)
349 (save-excursion
350 (goto-char beg)
351 (while (and (not found)
352 (<= (point) pt)
353 (< (point) end))
354 (and (re-search-forward thing-at-point-markedup-url-regexp
355 end 1)
356 (> (point) pt)
357 (setq found t))))
358 (if found
359 (cons (match-beginning 1) (match-end 1))))))
360
361(defun thing-at-point--bounds-of-well-formed-url (beg end pt)
362 (save-excursion
363 (goto-char beg)
364 (let (url-beg paren-end regexp)
365 (save-restriction
366 (narrow-to-region beg end)
367 ;; The scheme component must either match at BEG, or have no
368 ;; other alphanumerical ASCII characters before it.
369 (setq regexp (concat "\\(?:\\`\\|[^a-zA-Z0-9]\\)\\("
370 (or thing-at-point-beginning-of-url-regexp
371 (regexp-opt thing-at-point-uri-schemes))
372 "\\)"))
373 (and (re-search-forward regexp end t)
374 ;; URI must have non-empty contents.
375 (< (point) end)
376 (setq url-beg (match-beginning 1))))
377 (when url-beg
378 ;; If there is an open paren before the URI, truncate to the
379 ;; matching close paren.
380 (and (> url-beg (point-min))
381 (eq (car-safe (syntax-after (1- url-beg))) 4)
382 (save-restriction
383 (narrow-to-region (1- url-beg) (min end (point-max)))
384 (setq paren-end (ignore-errors
385 (scan-lists (1- url-beg) 1 0))))
386 (not (blink-matching-check-mismatch (1- url-beg) paren-end))
387 (setq end (1- paren-end)))
388 (cons url-beg end)))))
d9cc804b
RS
389
390(put 'url 'thing-at-point 'thing-at-point-url-at-point)
340483df 391
6e5c1569
CY
392(defun thing-at-point-url-at-point (&optional lax bounds)
393 "Return the URL around or before point.
394If no URL is found, return nil.
395
396If optional argument LAX is non-nil, look for URLs that are not
397well-formed, such as foo@bar or <nobody>.
398
399If optional arguments BOUNDS are non-nil, it should be a cons
400cell of the form (START . END), containing the beginning and end
401positions of the URI. Otherwise, these positions are detected
402automatically from the text around point.
403
404If the scheme component is absent, either because a URI delimited
405with <url:...> lacks one, or because an ill-formed URI was found
406with LAX or BEG and END, try to add a scheme in the returned URI.
407The scheme is chosen heuristically: \"mailto:\" if the address
408looks like an email address, \"ftp://\" if it starts with
409\"ftp\", etc."
410 (unless bounds
411 (setq bounds (thing-at-point-bounds-of-url-at-point lax)))
412 (when (and bounds (< (car bounds) (cdr bounds)))
413 (let ((str (buffer-substring-no-properties (car bounds) (cdr bounds))))
414 ;; If there is no scheme component, try to add one.
415 (unless (string-match "\\`[a-zA-Z][-a-zA-Z0-9+.]*:" str)
416 (or
417 ;; If the URI has the form <foo@bar>, treat it according to
418 ;; `thing-at-point-default-mail-uri-scheme'. If there are
419 ;; no angle brackets, it must be mailto.
420 (when (string-match "\\`[^:</>@]+@[-.0-9=&?$+A-Z_a-z~#,%;*]" str)
421 (let ((scheme (if (and (eq (char-before (car bounds)) ?<)
422 (eq (char-after (cdr bounds)) ?>))
423 thing-at-point-default-mail-uri-scheme
424 "mailto")))
425 (if scheme
426 (setq str (concat scheme ":" str)))))
427 ;; If the string is like <FOO>, where FOO is an existing user
428 ;; name on the system, treat that as an email address.
429 (and (string-match "\\`[[:alnum:]]+\\'" str)
430 (eq (char-before (car bounds)) ?<)
431 (eq (char-after (cdr bounds)) ?>)
432 (not (string-match "~" (expand-file-name (concat "~" str))))
433 (setq str (concat "mailto:" str)))
434 ;; If it looks like news.example.com, treat it as news.
435 (if (thing-at-point-newsgroup-p str)
436 (setq str (concat "news:" str)))
437 ;; If it looks like ftp.example.com. treat it as ftp.
438 (if (string-match "\\`ftp\\." str)
439 (setq str (concat "ftp://" str)))
440 ;; If it looks like www.example.com. treat it as http.
441 (if (string-match "\\`www\\." str)
442 (setq str (concat "http://" str)))
443 ;; Otherwise, it just isn't a URI.
444 (setq str nil)))
445 str)))
446
447(defun thing-at-point-newsgroup-p (string)
448 "Return STRING if it looks like a newsgroup name, else nil."
449 (and
450 (string-match thing-at-point-newsgroup-regexp string)
451 (let ((htbs '(gnus-active-hashtb gnus-newsrc-hashtb gnus-killed-hashtb))
452 (heads thing-at-point-newsgroup-heads)
453 htb ret)
454 (while htbs
455 (setq htb (car htbs) htbs (cdr htbs))
456 (condition-case nil
457 (progn
458 ;; errs: htb symbol may be unbound, or not a hash-table.
459 ;; gnus-gethash is just a macro for intern-soft.
460 (and (symbol-value htb)
461 (intern-soft string (symbol-value htb))
462 (setq ret string htbs nil))
463 ;; If we made it this far, gnus is running, so ignore "heads":
464 (setq heads nil))
465 (error nil)))
466 (or ret (not heads)
467 (let ((head (string-match "\\`\\([[:lower:]]+\\)\\." string)))
468 (and head (setq head (substring string 0 (match-end 1)))
469 (member head heads)
470 (setq ret string))))
471 ret)))
472
473(put 'url 'end-op (lambda () (end-of-thing 'url)))
474
475(put 'url 'beginning-op (lambda () (end-of-thing 'url)))
d9cc804b
RS
476
477;; The normal thingatpt mechanism doesn't work for complex regexps.
478;; This should work for almost any regexp wherever we are in the
479;; match. To do a perfect job for any arbitrary regexp would mean
480;; testing every position before point. Regexp searches won't find
481;; matches that straddle the start position so we search forwards once
482;; and then back repeatedly and then back up a char at a time.
483
484(defun thing-at-point-looking-at (regexp)
485 "Return non-nil if point is in or just after a match for REGEXP.
486Set the match data from the earliest such match ending at or after
487point."
488 (save-excursion
489 (let ((old-point (point)) match)
490 (and (looking-at regexp)
491 (>= (match-end 0) old-point)
492 (setq match (point)))
493 ;; Search back repeatedly from end of next match.
494 ;; This may fail if next match ends before this match does.
495 (re-search-forward regexp nil 'limit)
496 (while (and (re-search-backward regexp nil t)
497 (or (> (match-beginning 0) old-point)
498 (and (looking-at regexp) ; Extend match-end past search start
499 (>= (match-end 0) old-point)
500 (setq match (point))))))
501 (if (not match) nil
502 (goto-char match)
503 ;; Back up a char at a time in case search skipped
504 ;; intermediate match straddling search start pos.
505 (while (and (not (bobp))
506 (progn (backward-char 1) (looking-at regexp))
507 (>= (match-end 0) old-point)
508 (setq match (point))))
509 (goto-char match)
510 (looking-at regexp)))))
511
baef4cbe
KF
512;; Email addresses
513(defvar thing-at-point-email-regexp
86265518 514 "<?[-+_.~a-zA-Z][-+_.~:a-zA-Z0-9]*@[-.a-zA-Z0-9]+>?"
baef4cbe
KF
515 "A regular expression probably matching an email address.
516This does not match the real name portion, only the address, optionally
517with angle brackets.")
518
519;; Haven't set 'forward-op on 'email nor defined 'forward-email' because
520;; not sure they're actually needed, and URL seems to skip them too.
521;; Note that (end-of-thing 'email) and (beginning-of-thing 'email)
522;; work automagically, though.
523
524(put 'email 'bounds-of-thing-at-point
525 (lambda ()
526 (let ((thing (thing-at-point-looking-at thing-at-point-email-regexp)))
527 (if thing
528 (let ((beginning (match-beginning 0))
529 (end (match-end 0)))
530 (cons beginning end))))))
531
532(put 'email 'thing-at-point
533 (lambda ()
534 (let ((boundary-pair (bounds-of-thing-at-point 'email)))
535 (if boundary-pair
536 (buffer-substring-no-properties
537 (car boundary-pair) (cdr boundary-pair))))))
538
f1180544 539;; Buffer
1a2b6c52 540
206eef6c
SM
541(put 'buffer 'end-op (lambda () (goto-char (point-max))))
542(put 'buffer 'beginning-op (lambda () (goto-char (point-min))))
1a2b6c52 543
f1180544 544;; Aliases
1a2b6c52 545
f5bd0689
CY
546(defun word-at-point ()
547 "Return the word at point. See `thing-at-point'."
548 (thing-at-point 'word))
549
550(defun sentence-at-point ()
551 "Return the sentence at point. See `thing-at-point'."
552 (thing-at-point 'sentence))
1a2b6c52 553
c851323f 554(defun read-from-whole-string (str)
2a59b30d 555 "Read a Lisp expression from STR.
c851323f
RS
556Signal an error if the entire string was not used."
557 (let* ((read-data (read-from-string str))
f1180544 558 (more-left
1a2b6c52 559 (condition-case nil
1b0d40de 560 ;; The call to `ignore' suppresses a compiler warning.
7f1422bc 561 (progn (ignore (read-from-string (substring str (cdr read-data))))
1a2b6c52
RS
562 t)
563 (end-of-file nil))))
564 (if more-left
565 (error "Can't read whole string")
566 (car read-data))))
567
f1180544
JB
568(defun form-at-point (&optional thing pred)
569 (let ((sexp (condition-case nil
c851323f 570 (read-from-whole-string (thing-at-point (or thing 'sexp)))
1a2b6c52 571 (error nil))))
c851323f 572 (if (or (not pred) (funcall pred sexp)) sexp)))
1a2b6c52 573
be64abcf 574;;;###autoload
de285f27
CY
575(defun sexp-at-point ()
576 "Return the sexp at point, or nil if none is found."
577 (form-at-point 'sexp))
be64abcf 578;;;###autoload
2a59b30d 579(defun symbol-at-point ()
de285f27 580 "Return the symbol at point, or nil if none is found."
2a59b30d
SM
581 (let ((thing (thing-at-point 'symbol)))
582 (if thing (intern thing))))
be64abcf 583;;;###autoload
de285f27
CY
584(defun number-at-point ()
585 "Return the number at point, or nil if none is found."
586 (form-at-point 'sexp 'numberp))
748b0d84 587(put 'number 'thing-at-point 'number-at-point)
be64abcf 588;;;###autoload
de285f27
CY
589(defun list-at-point ()
590 "Return the Lisp list at point, or nil if none is found."
591 (form-at-point 'list 'listp))
1a2b6c52 592
55535639 593;;; thingatpt.el ends here