More minor changes in the Emacs manual
[emacs.git] / lisp / thingatpt.el
blob7fe99b0714c057fbe51017cfd727595d1750d216
1 ;;; thingatpt.el --- get the `thing' at point -*- lexical-binding:t -*-
3 ;; Copyright (C) 1991-1998, 2000-2018 Free Software Foundation, Inc.
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
10 ;; This file is part of GNU Emacs.
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.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
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.
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).
36 ;; Special cases are allowed for using properties associated with the named
37 ;; "thing":
39 ;; forward-op Function to call to skip forward over a "thing" (or
40 ;; with a negative argument, backward).
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".
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)
50 ;;; Code:
52 (provide 'thingatpt)
54 ;; Basic movement
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))))
69 ;; General routines
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'.
79 See the file `thingatpt.el' for documentation on how to define a
80 valid THING.
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))))))))))
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'.
137 When the optional argument NO-PROPERTIES is non-nil,
138 strip text properties from the return value.
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 (sequencep text))
149 (set-text-properties 0 (length text) nil text))
150 text))
152 ;; Go to beginning/end
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))))
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))))
168 ;; Special cases
170 ;; Lines
172 ;; bolp will be false when you click on the last line in the buffer
173 ;; and it has no final newline.
175 (put 'line 'beginning-op
176 (lambda () (if (bolp) (forward-line -1) (beginning-of-line))))
178 ;; Sexps
180 (defun 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)))))
188 (defun thing-at-point--end-of-sexp ()
189 "Move point to the end of the current sexp."
190 (let ((char-syntax (syntax-after (point))))
191 (if (or (eq char-syntax ?\))
192 (and (eq char-syntax ?\") (nth 3 (syntax-ppss))))
193 (forward-char 1)
194 (forward-sexp 1))))
196 (define-obsolete-function-alias 'end-of-sexp
197 'thing-at-point--end-of-sexp "25.1"
198 "This is an internal thingatpt function and should not be used.")
200 (put 'sexp 'end-op 'thing-at-point--end-of-sexp)
202 (defun thing-at-point--beginning-of-sexp ()
203 "Move point to the beginning of the current sexp."
204 (let ((char-syntax (char-syntax (char-before))))
205 (if (or (eq char-syntax ?\()
206 (and (eq char-syntax ?\") (nth 3 (syntax-ppss))))
207 (forward-char -1)
208 (forward-sexp -1))))
210 (define-obsolete-function-alias 'beginning-of-sexp
211 'thing-at-point--beginning-of-sexp "25.1"
212 "This is an internal thingatpt function and should not be used.")
214 (put 'sexp 'beginning-op 'thing-at-point--beginning-of-sexp)
216 ;; Lists
218 (put 'list 'bounds-of-thing-at-point 'thing-at-point-bounds-of-list-at-point)
220 (defun thing-at-point-bounds-of-list-at-point ()
221 "Return the bounds of the list at point.
222 \[Internal function used by `bounds-of-thing-at-point'.]"
223 (save-excursion
224 (let* ((st (parse-partial-sexp (point-min) (point)))
225 (beg (or (and (eq 4 (car (syntax-after (point))))
226 (not (nth 8 st))
227 (point))
228 (nth 1 st))))
229 (when beg
230 (goto-char beg)
231 (forward-sexp)
232 (cons beg (point))))))
234 ;; Defuns
236 (put 'defun 'beginning-op 'beginning-of-defun)
237 (put 'defun 'end-op 'end-of-defun)
238 (put 'defun 'forward-op 'end-of-defun)
240 ;; Filenames
242 (defvar thing-at-point-file-name-chars "-~/[:alnum:]_.${}#%,:"
243 "Characters allowable in filenames.")
245 (put 'filename 'end-op
246 (lambda ()
247 (re-search-forward (concat "\\=[" thing-at-point-file-name-chars "]*")
248 nil t)))
249 (put 'filename 'beginning-op
250 (lambda ()
251 (if (re-search-backward (concat "[^" thing-at-point-file-name-chars "]")
252 nil t)
253 (forward-char)
254 (goto-char (point-min)))))
256 ;; URIs
258 (defvar thing-at-point-beginning-of-url-regexp nil
259 "Regexp matching the beginning of a well-formed URI.
260 If nil, construct the regexp from `thing-at-point-uri-schemes'.")
262 (defvar thing-at-point-url-path-regexp
263 "[^]\t\n \"'<>[^`{}]*[^]\t\n \"'<>[^`{}.,;]+"
264 "Regexp matching the host and filename or e-mail part of a URL.")
266 (defvar thing-at-point-short-url-regexp
267 (concat "[-A-Za-z0-9]+\\.[-A-Za-z0-9.]+" thing-at-point-url-path-regexp)
268 "Regexp matching a URI without a scheme component.")
270 (defvar thing-at-point-uri-schemes
271 ;; Officials from http://www.iana.org/assignments/uri-schemes.html
272 '("aaa://" "about:" "acap://" "apt:" "bzr://" "bzr+ssh://"
273 "attachment:/" "chrome://" "cid:" "content://" "crid://" "cvs://"
274 "data:" "dav:" "dict://" "doi:" "dns:" "dtn:" "feed:" "file:/"
275 "finger://" "fish://" "ftp://" "geo:" "git://" "go:" "gopher://"
276 "h323:" "http://" "https://" "im:" "imap://" "info:" "ipp:"
277 "irc://" "irc6://" "ircs://" "iris.beep:" "jar:" "ldap://"
278 "ldaps://" "magnet:" "mailto:" "mid:" "mtqp://" "mupdate://"
279 "news:" "nfs://" "nntp://" "opaquelocktoken:" "pop://" "pres:"
280 "resource://" "rmi://" "rsync://" "rtsp://" "rtspu://" "service:"
281 "sftp://" "sip:" "sips:" "smb://" "sms:" "snmp://" "soap.beep://"
282 "soap.beeps://" "ssh://" "svn://" "svn+ssh://" "tag:" "tel:"
283 "telnet://" "tftp://" "tip://" "tn3270://" "udp://" "urn:"
284 "uuid:" "vemmi://" "webcal://" "xri://" "xmlrpc.beep://"
285 "xmlrpc.beeps://" "z39.50r://" "z39.50s://" "xmpp:"
286 ;; Compatibility
287 "fax:" "man:" "mms://" "mmsh://" "modem:" "prospero:" "snews:"
288 "wais://")
289 "List of URI schemes recognized by `thing-at-point-url-at-point'.
290 Each string in this list should correspond to the start of a
291 URI's scheme component, up to and including the trailing // if
292 the scheme calls for that to be present.")
294 (defvar thing-at-point-markedup-url-regexp "<URL:\\([^<>\n]+\\)>"
295 "Regexp matching a URL marked up per RFC1738.
296 This kind of markup was formerly recommended as a way to indicate
297 URIs, but as of RFC 3986 it is no longer recommended.
298 Subexpression 1 should contain the delimited URL.")
300 (defvar thing-at-point-newsgroup-regexp
301 "\\`[[:lower:]]+\\.[-+[:lower:]_0-9.]+\\'"
302 "Regexp matching a newsgroup name.")
304 (defvar thing-at-point-newsgroup-heads
305 '("alt" "comp" "gnu" "misc" "news" "sci" "soc" "talk")
306 "Used by `thing-at-point-newsgroup-p' if gnus is not running.")
308 (defvar thing-at-point-default-mail-uri-scheme "mailto"
309 "Default scheme for ill-formed URIs that look like <foo@example.com>.
310 If nil, do not give such URIs a scheme.")
312 (put 'url 'bounds-of-thing-at-point 'thing-at-point-bounds-of-url-at-point)
314 (defun thing-at-point-bounds-of-url-at-point (&optional lax)
315 "Return a cons cell containing the start and end of the URI at point.
316 Try to find a URI using `thing-at-point-markedup-url-regexp'.
317 If that fails, try with `thing-at-point-beginning-of-url-regexp'.
318 If that also fails, and optional argument LAX is non-nil, return
319 the bounds of a possible ill-formed URI (one lacking a scheme)."
320 ;; Look for the old <URL:foo> markup. If found, use it.
321 (or (thing-at-point--bounds-of-markedup-url)
322 ;; Otherwise, find the bounds within which a URI may exist. The
323 ;; method is similar to `ffap-string-at-point'. Note that URIs
324 ;; may contain parentheses but may not contain spaces (RFC3986).
325 (let* ((allowed-chars "--:=&?$+@-Z_[:alpha:]~#,%;*()!'")
326 (skip-before "^[0-9a-zA-Z]")
327 (skip-after ":;.,!?")
328 (pt (point))
329 (beg (save-excursion
330 (skip-chars-backward allowed-chars)
331 (skip-chars-forward skip-before pt)
332 (point)))
333 (end (save-excursion
334 (skip-chars-forward allowed-chars)
335 (skip-chars-backward skip-after pt)
336 (point))))
337 (or (thing-at-point--bounds-of-well-formed-url beg end pt)
338 (if lax (cons beg end))))))
340 (defun thing-at-point--bounds-of-markedup-url ()
341 (when thing-at-point-markedup-url-regexp
342 (let ((case-fold-search t)
343 (pt (point))
344 (beg (line-beginning-position))
345 (end (line-end-position))
346 found)
347 (save-excursion
348 (goto-char beg)
349 (while (and (not found)
350 (<= (point) pt)
351 (< (point) end))
352 (and (re-search-forward thing-at-point-markedup-url-regexp
353 end 1)
354 (> (point) pt)
355 (setq found t))))
356 (if found
357 (cons (match-beginning 1) (match-end 1))))))
359 (defun thing-at-point--bounds-of-well-formed-url (beg end pt)
360 (save-excursion
361 (goto-char beg)
362 (let (url-beg paren-end regexp)
363 (save-restriction
364 (narrow-to-region beg end)
365 ;; The scheme component must either match at BEG, or have no
366 ;; other alphanumerical ASCII characters before it.
367 (setq regexp (concat "\\(?:\\`\\|[^a-zA-Z0-9]\\)\\("
368 (or thing-at-point-beginning-of-url-regexp
369 (regexp-opt thing-at-point-uri-schemes))
370 "\\)"))
371 (and (re-search-forward regexp end t)
372 ;; URI must have non-empty contents.
373 (< (point) end)
374 (setq url-beg (match-beginning 1))))
375 (when url-beg
376 ;; If there is an open paren before the URI, truncate to the
377 ;; matching close paren.
378 (and (> url-beg (point-min))
379 (eq (car-safe (syntax-after (1- url-beg))) 4)
380 (save-restriction
381 (narrow-to-region (1- url-beg) (min end (point-max)))
382 (setq paren-end (ignore-errors
383 ;; Make the scan work inside comments.
384 (let ((parse-sexp-ignore-comments nil))
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 ;; Ensure PT is actually within BOUNDARY. Check the following
389 ;; example with point on the beginning of the line:
391 ;; 3,1406710489,https://gnu.org,0,"0"
392 (and (<= url-beg pt end) (cons url-beg end))))))
394 (put 'url 'thing-at-point 'thing-at-point-url-at-point)
396 (defun thing-at-point-url-at-point (&optional lax bounds)
397 "Return the URL around or before point.
398 If no URL is found, return nil.
400 If optional argument LAX is non-nil, look for URLs that are not
401 well-formed, such as foo@bar or <nobody>.
403 If optional arguments BOUNDS are non-nil, it should be a cons
404 cell of the form (START . END), containing the beginning and end
405 positions of the URI. Otherwise, these positions are detected
406 automatically from the text around point.
408 If the scheme component is absent, either because a URI delimited
409 with <url:...> lacks one, or because an ill-formed URI was found
410 with LAX or BEG and END, try to add a scheme in the returned URI.
411 The scheme is chosen heuristically: \"mailto:\" if the address
412 looks like an email address, \"ftp://\" if it starts with
413 \"ftp\", etc."
414 (unless bounds
415 (setq bounds (thing-at-point-bounds-of-url-at-point lax)))
416 (when (and bounds (< (car bounds) (cdr bounds)))
417 (let ((str (buffer-substring-no-properties (car bounds) (cdr bounds))))
418 ;; If there is no scheme component, try to add one.
419 (unless (string-match "\\`[a-zA-Z][-a-zA-Z0-9+.]*:" str)
421 ;; If the URI has the form <foo@bar>, treat it according to
422 ;; `thing-at-point-default-mail-uri-scheme'. If there are
423 ;; no angle brackets, it must be mailto.
424 (when (string-match "\\`[^:</>@]+@[-.0-9=&?$+A-Z_a-z~#,%;*]" str)
425 (let ((scheme (if (and (eq (char-before (car bounds)) ?<)
426 (eq (char-after (cdr bounds)) ?>))
427 thing-at-point-default-mail-uri-scheme
428 "mailto")))
429 (if scheme
430 (setq str (concat scheme ":" str)))))
431 ;; If the string is like <FOO>, where FOO is an existing user
432 ;; name on the system, treat that as an email address.
433 (and (string-match "\\`[[:alnum:]]+\\'" str)
434 (eq (char-before (car bounds)) ?<)
435 (eq (char-after (cdr bounds)) ?>)
436 (not (string-match "~" (expand-file-name (concat "~" str))))
437 (setq str (concat "mailto:" str)))
438 ;; If it looks like news.example.com, treat it as news.
439 (if (thing-at-point-newsgroup-p str)
440 (setq str (concat "news:" str)))
441 ;; If it looks like ftp.example.com. treat it as ftp.
442 (if (string-match "\\`ftp\\." str)
443 (setq str (concat "ftp://" str)))
444 ;; If it looks like www.example.com. treat it as http.
445 (if (string-match "\\`www\\." str)
446 (setq str (concat "http://" str)))
447 ;; Otherwise, it just isn't a URI.
448 (setq str nil)))
449 str)))
451 (defun thing-at-point-newsgroup-p (string)
452 "Return STRING if it looks like a newsgroup name, else nil."
453 (and
454 (string-match thing-at-point-newsgroup-regexp string)
455 (let ((htbs '(gnus-active-hashtb gnus-newsrc-hashtb gnus-killed-hashtb))
456 (heads thing-at-point-newsgroup-heads)
457 htb ret)
458 (while htbs
459 (setq htb (car htbs) htbs (cdr htbs))
460 (ignore-errors
461 ;; errs: htb symbol may be unbound, or not a hash-table.
462 ;; gnus-gethash is just a macro for intern-soft.
463 (and (symbol-value htb)
464 (intern-soft string (symbol-value htb))
465 (setq ret string htbs nil))
466 ;; If we made it this far, gnus is running, so ignore "heads":
467 (setq heads nil)))
468 (or ret (not heads)
469 (let ((head (string-match "\\`\\([[:lower:]]+\\)\\." string)))
470 (and head (setq head (substring string 0 (match-end 1)))
471 (member head heads)
472 (setq ret string))))
473 ret)))
475 (put 'url 'end-op (lambda () (end-of-thing 'url)))
477 (put 'url 'beginning-op (lambda () (end-of-thing 'url)))
479 ;; The normal thingatpt mechanism doesn't work for complex regexps.
480 ;; This should work for almost any regexp wherever we are in the
481 ;; match. To do a perfect job for any arbitrary regexp would mean
482 ;; testing every position before point. Regexp searches won't find
483 ;; matches that straddle the start position so we search forwards once
484 ;; and then back repeatedly and then back up a char at a time.
486 (defun thing-at-point-looking-at (regexp &optional distance)
487 "Return non-nil if point is in or just after a match for REGEXP.
488 Set the match data from the earliest such match ending at or after
489 point.
491 Optional argument DISTANCE limits search for REGEXP forward and
492 back from 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 prev-pos new-pos)
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 (setq prev-pos (point))
505 (while (and (setq new-pos (re-search-backward regexp backward-bound t))
506 ;; Avoid inflooping with some regexps, such as "^",
507 ;; matching which never moves point.
508 (< new-pos prev-pos)
509 (or (> (match-beginning 0) old-point)
510 (and (looking-at regexp) ; Extend match-end past search start
511 (>= (match-end 0) old-point)
512 (setq match (point))))))
513 (if (not match) nil
514 (goto-char match)
515 ;; Back up a char at a time in case search skipped
516 ;; intermediate match straddling search start pos.
517 (while (and (not (bobp))
518 (progn (backward-char 1) (looking-at regexp))
519 (>= (match-end 0) old-point)
520 (setq match (point))))
521 (goto-char match)
522 (looking-at regexp)))))
524 ;; Email addresses
525 (defvar thing-at-point-email-regexp
526 "<?[-+_.~a-zA-Z][-+_.~:a-zA-Z0-9]*@[-.a-zA-Z0-9]+>?"
527 "A regular expression probably matching an email address.
528 This does not match the real name portion, only the address, optionally
529 with angle brackets.")
531 ;; Haven't set 'forward-op on 'email nor defined 'forward-email' because
532 ;; not sure they're actually needed, and URL seems to skip them too.
533 ;; Note that (end-of-thing 'email) and (beginning-of-thing 'email)
534 ;; work automagically, though.
536 (put 'email 'bounds-of-thing-at-point
537 (lambda ()
538 (let ((thing (thing-at-point-looking-at
539 thing-at-point-email-regexp 500)))
540 (if thing
541 (let ((beginning (match-beginning 0))
542 (end (match-end 0)))
543 (cons beginning end))))))
545 (put 'email 'thing-at-point
546 (lambda ()
547 (let ((boundary-pair (bounds-of-thing-at-point 'email)))
548 (if boundary-pair
549 (buffer-substring-no-properties
550 (car boundary-pair) (cdr boundary-pair))))))
552 ;; Buffer
554 (put 'buffer 'end-op (lambda () (goto-char (point-max))))
555 (put 'buffer 'beginning-op (lambda () (goto-char (point-min))))
557 ;; Aliases
559 (defun word-at-point ()
560 "Return the word at point. See `thing-at-point'."
561 (thing-at-point 'word))
563 (defun sentence-at-point ()
564 "Return the sentence at point. See `thing-at-point'."
565 (thing-at-point 'sentence))
567 (defun thing-at-point--read-from-whole-string (str)
568 "Read a Lisp expression from STR.
569 Signal an error if the entire string was not used."
570 (let* ((read-data (read-from-string str))
571 (more-left
572 (condition-case nil
573 ;; The call to `ignore' suppresses a compiler warning.
574 (progn (ignore (read-from-string (substring str (cdr read-data))))
576 (end-of-file nil))))
577 (if more-left
578 (error "Can't read whole string")
579 (car read-data))))
581 (define-obsolete-function-alias 'read-from-whole-string
582 'thing-at-point--read-from-whole-string "25.1"
583 "This is an internal thingatpt function and should not be used.")
585 (defun form-at-point (&optional thing pred)
586 (let* ((obj (thing-at-point (or thing 'sexp)))
587 (sexp (if (stringp obj)
588 (ignore-errors
589 (thing-at-point--read-from-whole-string obj))
590 obj)))
591 (if (or (not pred) (funcall pred sexp)) sexp)))
593 ;;;###autoload
594 (defun sexp-at-point ()
595 "Return the sexp at point, or nil if none is found."
596 (form-at-point 'sexp))
597 ;;;###autoload
598 (defun symbol-at-point ()
599 "Return the symbol at point, or nil if none is found."
600 (let ((thing (thing-at-point 'symbol)))
601 (if thing (intern thing))))
602 ;;;###autoload
603 (defun number-at-point ()
604 "Return the number at point, or nil if none is found."
605 (when (thing-at-point-looking-at "-?[0-9]+\\.?[0-9]*" 500)
606 (string-to-number
607 (buffer-substring (match-beginning 0) (match-end 0)))))
609 (put 'number 'thing-at-point 'number-at-point)
610 ;;;###autoload
611 (defun list-at-point ()
612 "Return the Lisp list at point, or nil if none is found."
613 (form-at-point 'list 'listp))
615 ;;; thingatpt.el ends here