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