Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / thingatpt.el
blob5f9de9abbb2f6cb092a7b40f82eba92384a25bfb
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', `uuid', `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', `uuid', `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', `uuid', `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 Prefer the enclosing list with fallback on sexp at point.
226 \[Internal function used by `bounds-of-thing-at-point'.]"
227 (save-excursion
228 (if (ignore-errors (up-list -1))
229 (ignore-errors (cons (point) (progn (forward-sexp) (point))))
230 (let ((bound (bounds-of-thing-at-point 'sexp)))
231 (and bound
232 (<= (car bound) (point)) (< (point) (cdr bound))
233 bound)))))
235 ;; Defuns
237 (put 'defun 'beginning-op 'beginning-of-defun)
238 (put 'defun 'end-op 'end-of-defun)
239 (put 'defun 'forward-op 'end-of-defun)
241 ;; Things defined by sets of characters
243 (defmacro define-thing-chars (thing chars)
244 "Define THING as a sequence of CHARS.
245 E.g.:
246 \(define-thing-chars twitter-screen-name \"[:alnum:]_\")"
247 `(progn
248 (put ',thing 'end-op
249 (lambda ()
250 (re-search-forward (concat "\\=[" ,chars "]*") nil t)))
251 (put ',thing 'beginning-op
252 (lambda ()
253 (if (re-search-backward (concat "[^" ,chars "]") nil t)
254 (forward-char)
255 (goto-char (point-min)))))))
257 ;; Filenames
259 (defvar thing-at-point-file-name-chars "-~/[:alnum:]_.${}#%,:"
260 "Characters allowable in filenames.")
262 (define-thing-chars filename thing-at-point-file-name-chars)
264 ;; URIs
266 (defvar thing-at-point-beginning-of-url-regexp nil
267 "Regexp matching the beginning of a well-formed URI.
268 If nil, construct the regexp from `thing-at-point-uri-schemes'.")
270 (defvar thing-at-point-url-path-regexp
271 "[^]\t\n \"'<>[^`{}]*[^]\t\n \"'<>[^`{}.,;]+"
272 "Regexp matching the host and filename or e-mail part of a URL.")
274 (defvar thing-at-point-short-url-regexp
275 (concat "[-A-Za-z0-9]+\\.[-A-Za-z0-9.]+" thing-at-point-url-path-regexp)
276 "Regexp matching a URI without a scheme component.")
278 (defvar thing-at-point-uri-schemes
279 ;; Officials from http://www.iana.org/assignments/uri-schemes.html
280 '("aaa://" "about:" "acap://" "apt:" "bzr://" "bzr+ssh://"
281 "attachment:/" "chrome://" "cid:" "content://" "crid://" "cvs://"
282 "data:" "dav:" "dict://" "doi:" "dns:" "dtn:" "feed:" "file:/"
283 "finger://" "fish://" "ftp://" "geo:" "git://" "go:" "gopher://"
284 "h323:" "http://" "https://" "im:" "imap://" "info:" "ipp:"
285 "irc://" "irc6://" "ircs://" "iris.beep:" "jar:" "ldap://"
286 "ldaps://" "magnet:" "mailto:" "mid:" "mtqp://" "mupdate://"
287 "news:" "nfs://" "nntp://" "opaquelocktoken:" "pop://" "pres:"
288 "resource://" "rmi://" "rsync://" "rtsp://" "rtspu://" "service:"
289 "sftp://" "sip:" "sips:" "smb://" "sms:" "snmp://" "soap.beep://"
290 "soap.beeps://" "ssh://" "svn://" "svn+ssh://" "tag:" "tel:"
291 "telnet://" "tftp://" "tip://" "tn3270://" "udp://" "urn:"
292 "uuid:" "vemmi://" "webcal://" "xri://" "xmlrpc.beep://"
293 "xmlrpc.beeps://" "z39.50r://" "z39.50s://" "xmpp:"
294 ;; Compatibility
295 "fax:" "man:" "mms://" "mmsh://" "modem:" "prospero:" "snews:"
296 "wais://")
297 "List of URI schemes recognized by `thing-at-point-url-at-point'.
298 Each string in this list should correspond to the start of a
299 URI's scheme component, up to and including the trailing // if
300 the scheme calls for that to be present.")
302 (defvar thing-at-point-markedup-url-regexp "<URL:\\([^<>\n]+\\)>"
303 "Regexp matching a URL marked up per RFC1738.
304 This kind of markup was formerly recommended as a way to indicate
305 URIs, but as of RFC 3986 it is no longer recommended.
306 Subexpression 1 should contain the delimited URL.")
308 (defvar thing-at-point-newsgroup-regexp
309 "\\`[[:lower:]]+\\.[-+[:lower:]_0-9.]+\\'"
310 "Regexp matching a newsgroup name.")
312 (defvar thing-at-point-newsgroup-heads
313 '("alt" "comp" "gnu" "misc" "news" "sci" "soc" "talk")
314 "Used by `thing-at-point-newsgroup-p' if gnus is not running.")
316 (defvar thing-at-point-default-mail-uri-scheme "mailto"
317 "Default scheme for ill-formed URIs that look like <foo@example.com>.
318 If nil, do not give such URIs a scheme.")
320 (put 'url 'bounds-of-thing-at-point 'thing-at-point-bounds-of-url-at-point)
322 (defun thing-at-point-bounds-of-url-at-point (&optional lax)
323 "Return a cons cell containing the start and end of the URI at point.
324 Try to find a URI using `thing-at-point-markedup-url-regexp'.
325 If that fails, try with `thing-at-point-beginning-of-url-regexp'.
326 If that also fails, and optional argument LAX is non-nil, return
327 the bounds of a possible ill-formed URI (one lacking a scheme)."
328 ;; Look for the old <URL:foo> markup. If found, use it.
329 (or (thing-at-point--bounds-of-markedup-url)
330 ;; Otherwise, find the bounds within which a URI may exist. The
331 ;; method is similar to `ffap-string-at-point'. Note that URIs
332 ;; may contain parentheses but may not contain spaces (RFC3986).
333 (let* ((allowed-chars "--:=&?$+@-Z_[:alpha:]~#,%;*()!'")
334 (skip-before "^[0-9a-zA-Z]")
335 (skip-after ":;.,!?")
336 (pt (point))
337 (beg (save-excursion
338 (skip-chars-backward allowed-chars)
339 (skip-chars-forward skip-before pt)
340 (point)))
341 (end (save-excursion
342 (skip-chars-forward allowed-chars)
343 (skip-chars-backward skip-after pt)
344 (point))))
345 (or (thing-at-point--bounds-of-well-formed-url beg end pt)
346 (if lax (cons beg end))))))
348 (defun thing-at-point--bounds-of-markedup-url ()
349 (when thing-at-point-markedup-url-regexp
350 (let ((case-fold-search t)
351 (pt (point))
352 (beg (line-beginning-position))
353 (end (line-end-position))
354 found)
355 (save-excursion
356 (goto-char beg)
357 (while (and (not found)
358 (<= (point) pt)
359 (< (point) end))
360 (and (re-search-forward thing-at-point-markedup-url-regexp
361 end 1)
362 (> (point) pt)
363 (setq found t))))
364 (if found
365 (cons (match-beginning 1) (match-end 1))))))
367 (defun thing-at-point--bounds-of-well-formed-url (beg end pt)
368 (save-excursion
369 (goto-char beg)
370 (let (url-beg paren-end regexp)
371 (save-restriction
372 (narrow-to-region beg end)
373 ;; The scheme component must either match at BEG, or have no
374 ;; other alphanumerical ASCII characters before it.
375 (setq regexp (concat "\\(?:\\`\\|[^a-zA-Z0-9]\\)\\("
376 (or thing-at-point-beginning-of-url-regexp
377 (regexp-opt thing-at-point-uri-schemes))
378 "\\)"))
379 (and (re-search-forward regexp end t)
380 ;; URI must have non-empty contents.
381 (< (point) end)
382 (setq url-beg (match-beginning 1))))
383 (when url-beg
384 ;; If there is an open paren before the URI, truncate to the
385 ;; matching close paren.
386 (and (> url-beg (point-min))
387 (eq (car-safe (syntax-after (1- url-beg))) 4)
388 (save-restriction
389 (narrow-to-region (1- url-beg) (min end (point-max)))
390 (setq paren-end (ignore-errors
391 ;; Make the scan work inside comments.
392 (let ((parse-sexp-ignore-comments nil))
393 (scan-lists (1- url-beg) 1 0)))))
394 (not (blink-matching-check-mismatch (1- url-beg) paren-end))
395 (setq end (1- paren-end)))
396 ;; Ensure PT is actually within BOUNDARY. Check the following
397 ;; example with point on the beginning of the line:
399 ;; 3,1406710489,https://gnu.org,0,"0"
400 (and (<= url-beg pt end) (cons url-beg end))))))
402 (put 'url 'thing-at-point 'thing-at-point-url-at-point)
404 (defun thing-at-point-url-at-point (&optional lax bounds)
405 "Return the URL around or before point.
406 If no URL is found, return nil.
408 If optional argument LAX is non-nil, look for URLs that are not
409 well-formed, such as foo@bar or <nobody>.
411 If optional arguments BOUNDS are non-nil, it should be a cons
412 cell of the form (START . END), containing the beginning and end
413 positions of the URI. Otherwise, these positions are detected
414 automatically from the text around point.
416 If the scheme component is absent, either because a URI delimited
417 with <url:...> lacks one, or because an ill-formed URI was found
418 with LAX or BEG and END, try to add a scheme in the returned URI.
419 The scheme is chosen heuristically: \"mailto:\" if the address
420 looks like an email address, \"ftp://\" if it starts with
421 \"ftp\", etc."
422 (unless bounds
423 (setq bounds (thing-at-point-bounds-of-url-at-point lax)))
424 (when (and bounds (< (car bounds) (cdr bounds)))
425 (let ((str (buffer-substring-no-properties (car bounds) (cdr bounds))))
426 ;; If there is no scheme component, try to add one.
427 (unless (string-match "\\`[a-zA-Z][-a-zA-Z0-9+.]*:" str)
429 ;; If the URI has the form <foo@bar>, treat it according to
430 ;; `thing-at-point-default-mail-uri-scheme'. If there are
431 ;; no angle brackets, it must be mailto.
432 (when (string-match "\\`[^:</>@]+@[-.0-9=&?$+A-Z_a-z~#,%;*]" str)
433 (let ((scheme (if (and (eq (char-before (car bounds)) ?<)
434 (eq (char-after (cdr bounds)) ?>))
435 thing-at-point-default-mail-uri-scheme
436 "mailto")))
437 (if scheme
438 (setq str (concat scheme ":" str)))))
439 ;; If the string is like <FOO>, where FOO is an existing user
440 ;; name on the system, treat that as an email address.
441 (and (string-match "\\`[[:alnum:]]+\\'" str)
442 (eq (char-before (car bounds)) ?<)
443 (eq (char-after (cdr bounds)) ?>)
444 (not (string-match "~" (expand-file-name (concat "~" str))))
445 (setq str (concat "mailto:" str)))
446 ;; If it looks like news.example.com, treat it as news.
447 (if (thing-at-point-newsgroup-p str)
448 (setq str (concat "news:" str)))
449 ;; If it looks like ftp.example.com. treat it as ftp.
450 (if (string-match "\\`ftp\\." str)
451 (setq str (concat "ftp://" str)))
452 ;; If it looks like www.example.com. treat it as http.
453 (if (string-match "\\`www\\." str)
454 (setq str (concat "http://" str)))
455 ;; Otherwise, it just isn't a URI.
456 (setq str nil)))
457 str)))
459 (defun thing-at-point-newsgroup-p (string)
460 "Return STRING if it looks like a newsgroup name, else nil."
461 (and
462 (string-match thing-at-point-newsgroup-regexp string)
463 (let ((htbs '(gnus-active-hashtb gnus-newsrc-hashtb gnus-killed-hashtb))
464 (heads thing-at-point-newsgroup-heads)
465 htb ret)
466 (while htbs
467 (setq htb (car htbs) htbs (cdr htbs))
468 (ignore-errors
469 ;; errs: htb symbol may be unbound, or not a hash-table.
470 ;; gnus-gethash is just a macro for intern-soft.
471 (and (symbol-value htb)
472 (intern-soft string (symbol-value htb))
473 (setq ret string htbs nil))
474 ;; If we made it this far, gnus is running, so ignore "heads":
475 (setq heads nil)))
476 (or ret (not heads)
477 (let ((head (string-match "\\`\\([[:lower:]]+\\)\\." string)))
478 (and head (setq head (substring string 0 (match-end 1)))
479 (member head heads)
480 (setq ret string))))
481 ret)))
483 (put 'url 'end-op (lambda () (end-of-thing 'url)))
485 (put 'url 'beginning-op (lambda () (beginning-of-thing 'url)))
487 ;; The normal thingatpt mechanism doesn't work for complex regexps.
488 ;; This should work for almost any regexp wherever we are in the
489 ;; match. To do a perfect job for any arbitrary regexp would mean
490 ;; testing every position before point. Regexp searches won't find
491 ;; matches that straddle the start position so we search forwards once
492 ;; and then back repeatedly and then back up a char at a time.
494 (defun thing-at-point-looking-at (regexp &optional distance)
495 "Return non-nil if point is in or just after a match for REGEXP.
496 Set the match data from the earliest such match ending at or after
497 point.
499 Optional argument DISTANCE limits search for REGEXP forward and
500 back from point."
501 (save-excursion
502 (let ((old-point (point))
503 (forward-bound (and distance (+ (point) distance)))
504 (backward-bound (and distance (- (point) distance)))
505 match prev-pos new-pos)
506 (and (looking-at regexp)
507 (>= (match-end 0) old-point)
508 (setq match (point)))
509 ;; Search back repeatedly from end of next match.
510 ;; This may fail if next match ends before this match does.
511 (re-search-forward regexp forward-bound 'limit)
512 (setq prev-pos (point))
513 (while (and (setq new-pos (re-search-backward regexp backward-bound t))
514 ;; Avoid inflooping with some regexps, such as "^",
515 ;; matching which never moves point.
516 (< new-pos prev-pos)
517 (or (> (match-beginning 0) old-point)
518 (and (looking-at regexp) ; Extend match-end past search start
519 (>= (match-end 0) old-point)
520 (setq match (point))))))
521 (if (not match) nil
522 (goto-char match)
523 ;; Back up a char at a time in case search skipped
524 ;; intermediate match straddling search start pos.
525 (while (and (not (bobp))
526 (progn (backward-char 1) (looking-at regexp))
527 (>= (match-end 0) old-point)
528 (setq match (point))))
529 (goto-char match)
530 (looking-at regexp)))))
532 ;; Email addresses
533 (defvar thing-at-point-email-regexp
534 "<?[-+_.~a-zA-Z][-+_.~:a-zA-Z0-9]*@[-.a-zA-Z0-9]+>?"
535 "A regular expression probably matching an email address.
536 This does not match the real name portion, only the address, optionally
537 with angle brackets.")
539 ;; Haven't set 'forward-op on 'email nor defined 'forward-email' because
540 ;; not sure they're actually needed, and URL seems to skip them too.
541 ;; Note that (end-of-thing 'email) and (beginning-of-thing 'email)
542 ;; work automagically, though.
544 (put 'email 'bounds-of-thing-at-point
545 (lambda ()
546 (let ((thing (thing-at-point-looking-at
547 thing-at-point-email-regexp 500)))
548 (if thing
549 (let ((beginning (match-beginning 0))
550 (end (match-end 0)))
551 (cons beginning end))))))
553 (put 'email 'thing-at-point
554 (lambda ()
555 (let ((boundary-pair (bounds-of-thing-at-point 'email)))
556 (if boundary-pair
557 (buffer-substring-no-properties
558 (car boundary-pair) (cdr boundary-pair))))))
560 ;; Buffer
562 (put 'buffer 'end-op (lambda () (goto-char (point-max))))
563 (put 'buffer 'beginning-op (lambda () (goto-char (point-min))))
565 ;; UUID
567 (defconst thing-at-point-uuid-regexp
568 (rx bow
569 (repeat 8 hex-digit) "-"
570 (repeat 4 hex-digit) "-"
571 (repeat 4 hex-digit) "-"
572 (repeat 4 hex-digit) "-"
573 (repeat 12 hex-digit)
574 eow)
575 "A regular expression matching a UUID.
576 See RFC 4122 for the description of the format.")
578 (put 'uuid 'bounds-of-thing-at-point
579 (lambda ()
580 (when (thing-at-point-looking-at thing-at-point-uuid-regexp 36)
581 (cons (match-beginning 0) (match-end 0)))))
583 ;; Aliases
585 (defun word-at-point ()
586 "Return the word at point. See `thing-at-point'."
587 (thing-at-point 'word))
589 (defun sentence-at-point ()
590 "Return the sentence at point. See `thing-at-point'."
591 (thing-at-point 'sentence))
593 (defun thing-at-point--read-from-whole-string (str)
594 "Read a Lisp expression from STR.
595 Signal an error if the entire string was not used."
596 (let* ((read-data (read-from-string str))
597 (more-left
598 (condition-case nil
599 ;; The call to `ignore' suppresses a compiler warning.
600 (progn (ignore (read-from-string (substring str (cdr read-data))))
602 (end-of-file nil))))
603 (if more-left
604 (error "Can't read whole string")
605 (car read-data))))
607 (define-obsolete-function-alias 'read-from-whole-string
608 'thing-at-point--read-from-whole-string "25.1"
609 "This is an internal thingatpt function and should not be used.")
611 (defun form-at-point (&optional thing pred)
612 (let* ((obj (thing-at-point (or thing 'sexp)))
613 (sexp (if (stringp obj)
614 (ignore-errors
615 (thing-at-point--read-from-whole-string obj))
616 obj)))
617 (if (or (not pred) (funcall pred sexp)) sexp)))
619 ;;;###autoload
620 (defun sexp-at-point ()
621 "Return the sexp at point, or nil if none is found."
622 (form-at-point 'sexp))
623 ;;;###autoload
624 (defun symbol-at-point ()
625 "Return the symbol at point, or nil if none is found."
626 (let ((thing (thing-at-point 'symbol)))
627 (if thing (intern thing))))
628 ;;;###autoload
629 (defun number-at-point ()
630 "Return the number at point, or nil if none is found."
631 (when (thing-at-point-looking-at "-?[0-9]+\\.?[0-9]*" 500)
632 (string-to-number
633 (buffer-substring (match-beginning 0) (match-end 0)))))
635 (put 'number 'thing-at-point 'number-at-point)
636 ;;;###autoload
637 (defun list-at-point (&optional ignore-comment-or-string)
638 "Return the Lisp list at point, or nil if none is found.
639 If IGNORE-COMMENT-OR-STRING is non-nil comments and strings are
640 treated as white space."
641 (let ((ppss (and ignore-comment-or-string (syntax-ppss))))
642 (save-excursion
643 (goto-char (or (nth 8 ppss) (point)))
644 (form-at-point 'list 'listp))))
646 ;;; thingatpt.el ends here