Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; thingatpt.el --- get the `thing' at point |
1a2b6c52 | 2 | |
73b0cd50 | 3 | ;; Copyright (C) 1991-1998, 2000-2011 |
eb3fa2cf | 4 | ;; Free Software Foundation, Inc. |
1a2b6c52 RS |
5 | |
6 | ;; Author: Mike Williams <mikew@gopher.dosli.govt.nz> | |
6254fc9f | 7 | ;; Maintainer: FSF |
b7f66977 | 8 | ;; Keywords: extensions, matching, mouse |
1a2b6c52 | 9 | ;; Created: Thu Mar 28 13:48:23 1991 |
1a2b6c52 RS |
10 | |
11 | ;; This file is part of GNU Emacs. | |
12 | ||
eb3fa2cf | 13 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
1a2b6c52 | 14 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
15 | ;; the Free Software Foundation, either version 3 of the License, or |
16 | ;; (at your option) any later version. | |
1a2b6c52 RS |
17 | |
18 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;; GNU General Public License for more details. | |
22 | ||
eb3fa2cf GM |
23 | ;; You should have received a copy of the GNU General Public License |
24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
25 | ||
69f9ba7e | 26 | ;;; Commentary: |
b578f267 | 27 | |
c851323f RS |
28 | ;; This file provides routines for getting the "thing" at the location of |
29 | ;; point, whatever that "thing" happens to be. The "thing" is defined by | |
7a8f27db | 30 | ;; its beginning and end positions in the buffer. |
1a2b6c52 RS |
31 | ;; |
32 | ;; The function bounds-of-thing-at-point finds the beginning and end | |
c851323f | 33 | ;; positions by moving first forward to the end of the "thing", and then |
1a2b6c52 | 34 | ;; backwards to the beginning. By default, it uses the corresponding |
c851323f | 35 | ;; forward-"thing" operator (eg. forward-word, forward-line). |
1a2b6c52 RS |
36 | ;; |
37 | ;; Special cases are allowed for using properties associated with the named | |
f1180544 | 38 | ;; "thing": |
1a2b6c52 | 39 | ;; |
c851323f | 40 | ;; forward-op Function to call to skip forward over a "thing" (or |
1a2b6c52 | 41 | ;; with a negative argument, backward). |
f1180544 | 42 | ;; |
c851323f RS |
43 | ;; beginning-op Function to call to skip to the beginning of a "thing". |
44 | ;; end-op Function to call to skip to the end of a "thing". | |
1a2b6c52 RS |
45 | ;; |
46 | ;; Reliance on existing operators means that many `things' can be accessed | |
47 | ;; without further code: eg. | |
48 | ;; (thing-at-point 'line) | |
49 | ;; (thing-at-point 'page) | |
50 | ||
b578f267 | 51 | ;;; Code: |
1a2b6c52 RS |
52 | |
53 | (provide 'thingatpt) | |
54 | ||
b578f267 | 55 | ;; Basic movement |
1a2b6c52 RS |
56 | |
57 | ;;;###autoload | |
c851323f | 58 | (defun forward-thing (thing &optional n) |
2a59b30d | 59 | "Move forward to the end of the Nth next THING." |
c851323f RS |
60 | (let ((forward-op (or (get thing 'forward-op) |
61 | (intern-soft (format "forward-%s" thing))))) | |
6254fc9f | 62 | (if (functionp forward-op) |
c851323f RS |
63 | (funcall forward-op (or n 1)) |
64 | (error "Can't determine how to move over a %s" thing)))) | |
1a2b6c52 | 65 | |
b578f267 | 66 | ;; General routines |
1a2b6c52 RS |
67 | |
68 | ;;;###autoload | |
c851323f RS |
69 | (defun bounds-of-thing-at-point (thing) |
70 | "Determine the start and end buffer locations for the THING at point. | |
71 | THING is a symbol which specifies the kind of syntactic entity you want. | |
72 | Possibilities include `symbol', `list', `sexp', `defun', `filename', `url', | |
baef4cbe | 73 | `email', `word', `sentence', `whitespace', `line', `page' and others. |
c851323f RS |
74 | |
75 | See the file `thingatpt.el' for documentation on how to define | |
76 | a symbol as a valid THING. | |
77 | ||
78 | The value is a cons cell (START . END) giving the start and end positions | |
79 | of the textual entity that was found." | |
d9cc804b RS |
80 | (if (get thing 'bounds-of-thing-at-point) |
81 | (funcall (get thing 'bounds-of-thing-at-point)) | |
82 | (let ((orig (point))) | |
83 | (condition-case nil | |
84 | (save-excursion | |
85 | ;; Try moving forward, then back. | |
2a59b30d SM |
86 | (funcall ;; First move to end. |
87 | (or (get thing 'end-op) | |
88 | (lambda () (forward-thing thing 1)))) | |
89 | (funcall ;; Then move to beg. | |
90 | (or (get thing 'beginning-op) | |
91 | (lambda () (forward-thing thing -1)))) | |
92 | (let ((beg (point))) | |
d9cc804b RS |
93 | (if (not (and beg (> beg orig))) |
94 | ;; If that brings us all the way back to ORIG, | |
95 | ;; it worked. But END may not be the real end. | |
96 | ;; So find the real end that corresponds to BEG. | |
97 | (let ((real-end | |
f1180544 JB |
98 | (progn |
99 | (funcall | |
100 | (or (get thing 'end-op) | |
2a59b30d | 101 | (lambda () (forward-thing thing 1)))) |
d9cc804b RS |
102 | (point)))) |
103 | (if (and beg real-end (<= beg orig) (<= orig real-end)) | |
104 | (cons beg real-end))) | |
105 | (goto-char orig) | |
106 | ;; Try a second time, moving backward first and then forward, | |
107 | ;; so that we can find a thing that ends at ORIG. | |
2a59b30d SM |
108 | (funcall ;; First, move to beg. |
109 | (or (get thing 'beginning-op) | |
110 | (lambda () (forward-thing thing -1)))) | |
111 | (funcall ;; Then move to end. | |
112 | (or (get thing 'end-op) | |
113 | (lambda () (forward-thing thing 1)))) | |
114 | (let ((end (point)) | |
115 | (real-beg | |
f1180544 JB |
116 | (progn |
117 | (funcall | |
118 | (or (get thing 'beginning-op) | |
2a59b30d | 119 | (lambda () (forward-thing thing -1)))) |
d9cc804b RS |
120 | (point)))) |
121 | (if (and real-beg end (<= real-beg orig) (<= orig end)) | |
122 | (cons real-beg end)))))) | |
123 | (error nil))))) | |
1a2b6c52 RS |
124 | |
125 | ;;;###autoload | |
c851323f RS |
126 | (defun thing-at-point (thing) |
127 | "Return the THING at point. | |
128 | THING is a symbol which specifies the kind of syntactic entity you want. | |
129 | Possibilities include `symbol', `list', `sexp', `defun', `filename', `url', | |
baef4cbe | 130 | `email', `word', `sentence', `whitespace', `line', `page' and others. |
c851323f RS |
131 | |
132 | See the file `thingatpt.el' for documentation on how to define | |
133 | a symbol as a valid THING." | |
d9cc804b RS |
134 | (if (get thing 'thing-at-point) |
135 | (funcall (get thing 'thing-at-point)) | |
136 | (let ((bounds (bounds-of-thing-at-point thing))) | |
f1180544 | 137 | (if bounds |
d9cc804b | 138 | (buffer-substring (car bounds) (cdr bounds)))))) |
1a2b6c52 | 139 | |
b578f267 | 140 | ;; Go to beginning/end |
1a2b6c52 | 141 | |
c851323f RS |
142 | (defun beginning-of-thing (thing) |
143 | (let ((bounds (bounds-of-thing-at-point thing))) | |
144 | (or bounds (error "No %s here" thing)) | |
1a2b6c52 RS |
145 | (goto-char (car bounds)))) |
146 | ||
c851323f RS |
147 | (defun end-of-thing (thing) |
148 | (let ((bounds (bounds-of-thing-at-point thing))) | |
149 | (or bounds (error "No %s here" thing)) | |
1a2b6c52 RS |
150 | (goto-char (cdr bounds)))) |
151 | ||
f1180544 | 152 | ;; Special cases |
1a2b6c52 | 153 | |
f1180544 | 154 | ;; Lines |
9f5c7ace RS |
155 | |
156 | ;; bolp will be false when you click on the last line in the buffer | |
157 | ;; and it has no final newline. | |
158 | ||
159 | (put 'line 'beginning-op | |
2a59b30d | 160 | (lambda () (if (bolp) (forward-line -1) (beginning-of-line)))) |
9f5c7ace | 161 | |
f1180544 | 162 | ;; Sexps |
1a2b6c52 RS |
163 | |
164 | (defun in-string-p () | |
165 | (let ((orig (point))) | |
166 | (save-excursion | |
167 | (beginning-of-defun) | |
168 | (nth 3 (parse-partial-sexp (point) orig))))) | |
169 | ||
170 | (defun end-of-sexp () | |
ce8fb8aa | 171 | (let ((char-syntax (char-syntax (char-after)))) |
1a2b6c52 RS |
172 | (if (or (eq char-syntax ?\)) |
173 | (and (eq char-syntax ?\") (in-string-p))) | |
174 | (forward-char 1) | |
175 | (forward-sexp 1)))) | |
176 | ||
177 | (put 'sexp 'end-op 'end-of-sexp) | |
178 | ||
6f0e09d4 | 179 | (defun beginning-of-sexp () |
ce8fb8aa | 180 | (let ((char-syntax (char-syntax (char-before)))) |
6f0e09d4 RS |
181 | (if (or (eq char-syntax ?\() |
182 | (and (eq char-syntax ?\") (in-string-p))) | |
183 | (forward-char -1) | |
184 | (forward-sexp -1)))) | |
185 | ||
186 | (put 'sexp 'beginning-op 'beginning-of-sexp) | |
187 | ||
f1180544 | 188 | ;; Lists |
1a2b6c52 | 189 | |
965b9376 CY |
190 | (put 'list 'bounds-of-thing-at-point 'thing-at-point-bounds-of-list-at-point) |
191 | ||
192 | (defun thing-at-point-bounds-of-list-at-point () | |
193 | (save-excursion | |
194 | (let ((opoint (point)) | |
195 | (beg (condition-case nil | |
196 | (progn (up-list -1) | |
197 | (point)) | |
198 | (error nil)))) | |
199 | (condition-case nil | |
200 | (if beg | |
201 | (progn (forward-sexp) | |
202 | (cons beg (point))) | |
203 | ;; Are we are at the beginning of a top-level sexp? | |
204 | (forward-sexp) | |
205 | (let ((end (point))) | |
206 | (backward-sexp) | |
207 | (if (>= opoint (point)) | |
208 | (cons opoint end)))) | |
209 | (error nil))))) | |
1a2b6c52 | 210 | |
4d61e7d5 | 211 | ;; Filenames and URLs www.com/foo%32bar |
1a2b6c52 | 212 | |
839aacc9 | 213 | (defvar thing-at-point-file-name-chars "-~/[:alnum:]_.${}#%,:" |
1a2b6c52 RS |
214 | "Characters allowable in filenames.") |
215 | ||
f1180544 | 216 | (put 'filename 'end-op |
839aacc9 DL |
217 | (lambda () |
218 | (re-search-forward (concat "\\=[" thing-at-point-file-name-chars "]*") | |
219 | nil t))) | |
1a2b6c52 | 220 | (put 'filename 'beginning-op |
839aacc9 DL |
221 | (lambda () |
222 | (if (re-search-backward (concat "[^" thing-at-point-file-name-chars "]") | |
223 | nil t) | |
224 | (forward-char) | |
225 | (goto-char (point-min))))) | |
c851323f | 226 | |
d9cc804b | 227 | (defvar thing-at-point-url-path-regexp |
42986283 | 228 | "[^]\t\n \"'<>[^`{}]*[^]\t\n \"'<>[^`{}.,;]+" |
0408f074 | 229 | "A regular expression probably matching the host and filename or e-mail part of a URL.") |
d9cc804b RS |
230 | |
231 | (defvar thing-at-point-short-url-regexp | |
232 | (concat "[-A-Za-z0-9.]+" thing-at-point-url-path-regexp) | |
233 | "A regular expression probably matching a URL without an access scheme. | |
234 | Hostname matching is stricter in this case than for | |
235 | ``thing-at-point-url-regexp''.") | |
236 | ||
1c1766c7 | 237 | (defvar thing-at-point-uri-schemes |
4f5d4668 | 238 | ;; Officials from http://www.iana.org/assignments/uri-schemes.html |
1c1766c7 MR |
239 | '("ftp://" "http://" "gopher://" "mailto:" "news:" "nntp:" |
240 | "telnet://" "wais://" "file:/" "prospero:" "z39.50s:" "z39.50r:" | |
241 | "cid:" "mid:" "vemmi:" "service:" "imap:" "nfs:" "acap:" "rtsp:" | |
242 | "tip:" "pop:" "data:" "dav:" "opaquelocktoken:" "sip:" "tel:" "fax:" | |
243 | "modem:" "ldap:" "https://" "soap.beep:" "soap.beeps:" "urn:" "go:" | |
244 | "afs:" "tn3270:" "mailserver:" | |
4f5d4668 RS |
245 | "crid:" "dict:" "dns:" "dtn:" "h323:" "im:" "info:" "ipp:" |
246 | "iris.beep:" "mtqp:" "mupdate:" "pres:" "sips:" "snmp:" "tag:" | |
247 | "tftp:" "xmlrpc.beep:" "xmlrpc.beeps:" "xmpp:" | |
1c1766c7 | 248 | ;; Compatibility |
4f5d4668 | 249 | "snews:" "irc:" "mms://" "mmsh://") |
2a59b30d | 250 | "Uniform Resource Identifier (URI) Schemes.") |
1c1766c7 | 251 | |
d9cc804b | 252 | (defvar thing-at-point-url-regexp |
1c1766c7 MR |
253 | (concat "\\<\\(" (mapconcat 'identity thing-at-point-uri-schemes "\\|") "\\)" |
254 | thing-at-point-url-path-regexp) | |
d9cc804b RS |
255 | "A regular expression probably matching a complete URL.") |
256 | ||
257 | (defvar thing-at-point-markedup-url-regexp | |
258 | "<URL:[^>]+>" | |
259 | "A regular expression matching a URL marked up per RFC1738. | |
260 | This may contain whitespace (including newlines) .") | |
261 | ||
262 | (put 'url 'bounds-of-thing-at-point 'thing-at-point-bounds-of-url-at-point) | |
263 | (defun thing-at-point-bounds-of-url-at-point () | |
2a59b30d SM |
264 | (let ((strip (thing-at-point-looking-at |
265 | thing-at-point-markedup-url-regexp))) ;; (url "") short | |
266 | (if (or strip | |
883d1f4b | 267 | (thing-at-point-looking-at thing-at-point-url-regexp) |
d9cc804b | 268 | ;; Access scheme omitted? |
2a59b30d SM |
269 | ;; (setq short (thing-at-point-looking-at |
270 | ;; thing-at-point-short-url-regexp)) | |
271 | ) | |
d9cc804b RS |
272 | (let ((beginning (match-beginning 0)) |
273 | (end (match-end 0))) | |
2a59b30d SM |
274 | (when strip |
275 | (setq beginning (+ beginning 5)) | |
276 | (setq end (- end 1))) | |
d9cc804b RS |
277 | (cons beginning end))))) |
278 | ||
279 | (put 'url 'thing-at-point 'thing-at-point-url-at-point) | |
280 | (defun thing-at-point-url-at-point () | |
281 | "Return the URL around or before point. | |
340483df DL |
282 | |
283 | Search backwards for the start of a URL ending at or after point. If | |
284 | no URL found, return nil. The access scheme will be prepended if | |
285 | absent: \"mailto:\" if the string contains \"@\", \"ftp://\" if it | |
286 | starts with \"ftp\" and not \"ftp:/\", or \"http://\" by default." | |
287 | ||
d9cc804b RS |
288 | (let ((url "") short strip) |
289 | (if (or (setq strip (thing-at-point-looking-at | |
290 | thing-at-point-markedup-url-regexp)) | |
291 | (thing-at-point-looking-at thing-at-point-url-regexp) | |
292 | ;; Access scheme omitted? | |
293 | (setq short (thing-at-point-looking-at | |
294 | thing-at-point-short-url-regexp))) | |
295 | (progn | |
296 | (setq url (buffer-substring-no-properties (match-beginning 0) | |
297 | (match-end 0))) | |
298 | (and strip (setq url (substring url 5 -1))) ; Drop "<URL:" & ">" | |
299 | ;; strip whitespace | |
84841dd1 | 300 | (while (string-match "[ \t\n\r]+" url) |
d9cc804b | 301 | (setq url (replace-match "" t t url))) |
4f5d4668 RS |
302 | (and short (setq url (concat (cond ((string-match "^[a-zA-Z]+:" url) |
303 | ;; already has a URL scheme. | |
304 | "") | |
305 | ((string-match "@" url) | |
340483df DL |
306 | "mailto:") |
307 | ;; e.g. ftp.swiss... or ftp-swiss... | |
308 | ((string-match "^ftp" url) | |
309 | "ftp://") | |
310 | (t "http://")) | |
311 | url))) | |
d9cc804b RS |
312 | (if (string-equal "" url) |
313 | nil | |
314 | url))))) | |
315 | ||
316 | ;; The normal thingatpt mechanism doesn't work for complex regexps. | |
317 | ;; This should work for almost any regexp wherever we are in the | |
318 | ;; match. To do a perfect job for any arbitrary regexp would mean | |
319 | ;; testing every position before point. Regexp searches won't find | |
320 | ;; matches that straddle the start position so we search forwards once | |
321 | ;; and then back repeatedly and then back up a char at a time. | |
322 | ||
323 | (defun thing-at-point-looking-at (regexp) | |
324 | "Return non-nil if point is in or just after a match for REGEXP. | |
325 | Set the match data from the earliest such match ending at or after | |
326 | point." | |
327 | (save-excursion | |
328 | (let ((old-point (point)) match) | |
329 | (and (looking-at regexp) | |
330 | (>= (match-end 0) old-point) | |
331 | (setq match (point))) | |
332 | ;; Search back repeatedly from end of next match. | |
333 | ;; This may fail if next match ends before this match does. | |
334 | (re-search-forward regexp nil 'limit) | |
335 | (while (and (re-search-backward regexp nil t) | |
336 | (or (> (match-beginning 0) old-point) | |
337 | (and (looking-at regexp) ; Extend match-end past search start | |
338 | (>= (match-end 0) old-point) | |
339 | (setq match (point)))))) | |
340 | (if (not match) nil | |
341 | (goto-char match) | |
342 | ;; Back up a char at a time in case search skipped | |
343 | ;; intermediate match straddling search start pos. | |
344 | (while (and (not (bobp)) | |
345 | (progn (backward-char 1) (looking-at regexp)) | |
346 | (>= (match-end 0) old-point) | |
347 | (setq match (point)))) | |
348 | (goto-char match) | |
349 | (looking-at regexp))))) | |
350 | ||
a1c9b4d0 | 351 | (put 'url 'end-op |
2a59b30d SM |
352 | (lambda () |
353 | (let ((bounds (thing-at-point-bounds-of-url-at-point))) | |
354 | (if bounds | |
355 | (goto-char (cdr bounds)) | |
356 | (error "No URL here"))))) | |
c851323f | 357 | (put 'url 'beginning-op |
2a59b30d SM |
358 | (lambda () |
359 | (let ((bounds (thing-at-point-bounds-of-url-at-point))) | |
360 | (if bounds | |
361 | (goto-char (car bounds)) | |
362 | (error "No URL here"))))) | |
1a2b6c52 | 363 | |
baef4cbe KF |
364 | ;; Email addresses |
365 | (defvar thing-at-point-email-regexp | |
86265518 | 366 | "<?[-+_.~a-zA-Z][-+_.~:a-zA-Z0-9]*@[-.a-zA-Z0-9]+>?" |
baef4cbe KF |
367 | "A regular expression probably matching an email address. |
368 | This does not match the real name portion, only the address, optionally | |
369 | with angle brackets.") | |
370 | ||
371 | ;; Haven't set 'forward-op on 'email nor defined 'forward-email' because | |
372 | ;; not sure they're actually needed, and URL seems to skip them too. | |
373 | ;; Note that (end-of-thing 'email) and (beginning-of-thing 'email) | |
374 | ;; work automagically, though. | |
375 | ||
376 | (put 'email 'bounds-of-thing-at-point | |
377 | (lambda () | |
378 | (let ((thing (thing-at-point-looking-at thing-at-point-email-regexp))) | |
379 | (if thing | |
380 | (let ((beginning (match-beginning 0)) | |
381 | (end (match-end 0))) | |
382 | (cons beginning end)))))) | |
383 | ||
384 | (put 'email 'thing-at-point | |
385 | (lambda () | |
386 | (let ((boundary-pair (bounds-of-thing-at-point 'email))) | |
387 | (if boundary-pair | |
388 | (buffer-substring-no-properties | |
389 | (car boundary-pair) (cdr boundary-pair)))))) | |
390 | ||
f1180544 | 391 | ;; Whitespace |
1a2b6c52 | 392 | |
c851323f | 393 | (defun forward-whitespace (arg) |
1a2b6c52 | 394 | (interactive "p") |
f1180544 | 395 | (if (natnump arg) |
9e594a2e | 396 | (re-search-forward "[ \t]+\\|\n" nil 'move arg) |
c851323f | 397 | (while (< arg 0) |
9e594a2e | 398 | (if (re-search-backward "[ \t]+\\|\n" nil 'move) |
1a2b6c52 RS |
399 | (or (eq (char-after (match-beginning 0)) 10) |
400 | (skip-chars-backward " \t"))) | |
c851323f | 401 | (setq arg (1+ arg))))) |
1a2b6c52 | 402 | |
f1180544 | 403 | ;; Buffer |
1a2b6c52 | 404 | |
206eef6c SM |
405 | (put 'buffer 'end-op (lambda () (goto-char (point-max)))) |
406 | (put 'buffer 'beginning-op (lambda () (goto-char (point-min)))) | |
1a2b6c52 | 407 | |
f1180544 | 408 | ;; Symbols |
1a2b6c52 | 409 | |
c851323f | 410 | (defun forward-symbol (arg) |
1a2b6c52 | 411 | (interactive "p") |
f1180544 | 412 | (if (natnump arg) |
9e594a2e | 413 | (re-search-forward "\\(\\sw\\|\\s_\\)+" nil 'move arg) |
c851323f | 414 | (while (< arg 0) |
9e594a2e | 415 | (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil 'move) |
1a2b6c52 | 416 | (skip-syntax-backward "w_")) |
c851323f | 417 | (setq arg (1+ arg))))) |
1a2b6c52 | 418 | |
f1180544 | 419 | ;; Syntax blocks |
fde7326e RS |
420 | |
421 | (defun forward-same-syntax (&optional arg) | |
422 | (interactive "p") | |
423 | (while (< arg 0) | |
f1180544 | 424 | (skip-syntax-backward |
ce8fb8aa | 425 | (char-to-string (char-syntax (char-before)))) |
fde7326e RS |
426 | (setq arg (1+ arg))) |
427 | (while (> arg 0) | |
ce8fb8aa | 428 | (skip-syntax-forward (char-to-string (char-syntax (char-after)))) |
fde7326e RS |
429 | (setq arg (1- arg)))) |
430 | ||
f1180544 | 431 | ;; Aliases |
1a2b6c52 RS |
432 | |
433 | (defun word-at-point () (thing-at-point 'word)) | |
434 | (defun sentence-at-point () (thing-at-point 'sentence)) | |
435 | ||
c851323f | 436 | (defun read-from-whole-string (str) |
2a59b30d | 437 | "Read a Lisp expression from STR. |
c851323f RS |
438 | Signal an error if the entire string was not used." |
439 | (let* ((read-data (read-from-string str)) | |
f1180544 | 440 | (more-left |
1a2b6c52 | 441 | (condition-case nil |
1b0d40de | 442 | ;; The call to `ignore' suppresses a compiler warning. |
7f1422bc | 443 | (progn (ignore (read-from-string (substring str (cdr read-data)))) |
1a2b6c52 RS |
444 | t) |
445 | (end-of-file nil)))) | |
446 | (if more-left | |
447 | (error "Can't read whole string") | |
448 | (car read-data)))) | |
449 | ||
f1180544 JB |
450 | (defun form-at-point (&optional thing pred) |
451 | (let ((sexp (condition-case nil | |
c851323f | 452 | (read-from-whole-string (thing-at-point (or thing 'sexp))) |
1a2b6c52 | 453 | (error nil)))) |
c851323f | 454 | (if (or (not pred) (funcall pred sexp)) sexp))) |
1a2b6c52 | 455 | |
be64abcf | 456 | ;;;###autoload |
de285f27 CY |
457 | (defun sexp-at-point () |
458 | "Return the sexp at point, or nil if none is found." | |
459 | (form-at-point 'sexp)) | |
be64abcf | 460 | ;;;###autoload |
2a59b30d | 461 | (defun symbol-at-point () |
de285f27 | 462 | "Return the symbol at point, or nil if none is found." |
2a59b30d SM |
463 | (let ((thing (thing-at-point 'symbol))) |
464 | (if thing (intern thing)))) | |
be64abcf | 465 | ;;;###autoload |
de285f27 CY |
466 | (defun number-at-point () |
467 | "Return the number at point, or nil if none is found." | |
468 | (form-at-point 'sexp 'numberp)) | |
be64abcf | 469 | ;;;###autoload |
de285f27 CY |
470 | (defun list-at-point () |
471 | "Return the Lisp list at point, or nil if none is found." | |
472 | (form-at-point 'list 'listp)) | |
1a2b6c52 | 473 | |
55535639 | 474 | ;;; thingatpt.el ends here |