thingatpt.el 23 KB

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