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