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