1 ;;; thingatpt.el --- get the `thing' at point
3 ;; Copyright (C) 1991-1998, 2000-2011 Free Software Foundation, Inc.
5 ;; Author: Mike Williams <mikew@gopher.dosli.govt.nz>
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 <http://www.gnu.org/licenses/>.
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
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 ;; 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)
57 (defun forward-thing (thing &optional n
)
58 "Move forward to the end of the Nth next THING."
59 (let ((forward-op (or (get thing
'forward-op
)
60 (intern-soft (format "forward-%s" thing
)))))
61 (if (functionp forward-op
)
62 (funcall forward-op
(or n
1))
63 (error "Can't determine how to move over a %s" thing
))))
68 (defun bounds-of-thing-at-point (thing)
69 "Determine the start and end buffer locations for the THING at point.
70 THING is a symbol which specifies the kind of syntactic entity you want.
71 Possibilities include `symbol', `list', `sexp', `defun', `filename', `url',
72 `email', `word', `sentence', `whitespace', `line', `page' and others.
74 See the file `thingatpt.el' for documentation on how to define
75 a symbol as a valid THING.
77 The value is a cons cell (START . END) giving the start and end positions
78 of the textual entity that was found."
79 (if (get thing
'bounds-of-thing-at-point
)
80 (funcall (get thing
'bounds-of-thing-at-point
))
84 ;; Try moving forward, then back.
85 (funcall ;; First move to end.
86 (or (get thing
'end-op
)
87 (lambda () (forward-thing thing
1))))
88 (funcall ;; Then move to beg.
89 (or (get thing
'beginning-op
)
90 (lambda () (forward-thing thing -
1))))
92 (if (not (and beg
(> beg orig
)))
93 ;; If that brings us all the way back to ORIG,
94 ;; it worked. But END may not be the real end.
95 ;; So find the real end that corresponds to BEG.
99 (or (get thing
'end-op
)
100 (lambda () (forward-thing thing
1))))
102 (if (and beg real-end
(<= beg orig
) (<= orig real-end
))
103 (cons beg real-end
)))
105 ;; Try a second time, moving backward first and then forward,
106 ;; so that we can find a thing that ends at ORIG.
107 (funcall ;; First, move to beg.
108 (or (get thing
'beginning-op
)
109 (lambda () (forward-thing thing -
1))))
110 (funcall ;; Then move to end.
111 (or (get thing
'end-op
)
112 (lambda () (forward-thing thing
1))))
117 (or (get thing
'beginning-op
)
118 (lambda () (forward-thing thing -
1))))
120 (if (and real-beg end
(<= real-beg orig
) (<= orig end
))
121 (cons real-beg end
))))))
125 (defun thing-at-point (thing)
126 "Return the THING at point.
127 THING is a symbol which specifies the kind of syntactic entity you want.
128 Possibilities include `symbol', `list', `sexp', `defun', `filename', `url',
129 `email', `word', `sentence', `whitespace', `line', `page' and others.
131 See the file `thingatpt.el' for documentation on how to define
132 a symbol as a valid THING."
133 (if (get thing
'thing-at-point
)
134 (funcall (get thing
'thing-at-point
))
135 (let ((bounds (bounds-of-thing-at-point thing
)))
137 (buffer-substring (car bounds
) (cdr bounds
))))))
139 ;; Go to beginning/end
141 (defun beginning-of-thing (thing)
142 (let ((bounds (bounds-of-thing-at-point thing
)))
143 (or bounds
(error "No %s here" thing
))
144 (goto-char (car bounds
))))
146 (defun end-of-thing (thing)
147 (let ((bounds (bounds-of-thing-at-point thing
)))
148 (or bounds
(error "No %s here" thing
))
149 (goto-char (cdr bounds
))))
155 ;; bolp will be false when you click on the last line in the buffer
156 ;; and it has no final newline.
158 (put 'line
'beginning-op
159 (lambda () (if (bolp) (forward-line -
1) (beginning-of-line))))
163 (defun in-string-p ()
164 (let ((orig (point)))
167 (nth 3 (parse-partial-sexp (point) orig
)))))
169 (defun end-of-sexp ()
170 (let ((char-syntax (char-syntax (char-after))))
171 (if (or (eq char-syntax ?\
))
172 (and (eq char-syntax ?
\") (in-string-p)))
176 (put 'sexp
'end-op
'end-of-sexp
)
178 (defun beginning-of-sexp ()
179 (let ((char-syntax (char-syntax (char-before))))
180 (if (or (eq char-syntax ?\
()
181 (and (eq char-syntax ?
\") (in-string-p)))
185 (put 'sexp
'beginning-op
'beginning-of-sexp
)
189 (put 'list
'bounds-of-thing-at-point
'thing-at-point-bounds-of-list-at-point
)
191 (defun thing-at-point-bounds-of-list-at-point ()
193 (let ((opoint (point))
194 (beg (condition-case nil
200 (progn (forward-sexp)
202 ;; Are we are at the beginning of a top-level sexp?
206 (if (>= opoint
(point))
212 (put 'defun
'beginning-op
'beginning-of-defun
)
213 (put 'defun
'end-op
'end-of-defun
)
214 (put 'defun
'forward-op
'end-of-defun
)
216 ;; Filenames and URLs www.com/foo%32bar
218 (defvar thing-at-point-file-name-chars
"-~/[:alnum:]_.${}#%,:"
219 "Characters allowable in filenames.")
221 (put 'filename
'end-op
223 (re-search-forward (concat "\\=[" thing-at-point-file-name-chars
"]*")
225 (put 'filename
'beginning-op
227 (if (re-search-backward (concat "[^" thing-at-point-file-name-chars
"]")
230 (goto-char (point-min)))))
232 (defvar thing-at-point-url-path-regexp
233 "[^]\t\n \"'<>[^`{}]*[^]\t\n \"'<>[^`{}.,;]+"
234 "A regular expression probably matching the host and filename or e-mail part of a URL.")
236 (defvar thing-at-point-short-url-regexp
237 (concat "[-A-Za-z0-9.]+" thing-at-point-url-path-regexp
)
238 "A regular expression probably matching a URL without an access scheme.
239 Hostname matching is stricter in this case than for
240 ``thing-at-point-url-regexp''.")
242 (defvar thing-at-point-uri-schemes
243 ;; Officials from http://www.iana.org/assignments/uri-schemes.html
244 '("ftp://" "http://" "gopher://" "mailto:" "news:" "nntp:"
245 "telnet://" "wais://" "file:/" "prospero:" "z39.50s:" "z39.50r:"
246 "cid:" "mid:" "vemmi:" "service:" "imap:" "nfs:" "acap:" "rtsp:"
247 "tip:" "pop:" "data:" "dav:" "opaquelocktoken:" "sip:" "tel:" "fax:"
248 "modem:" "ldap:" "https://" "soap.beep:" "soap.beeps:" "urn:" "go:"
249 "afs:" "tn3270:" "mailserver:"
250 "crid:" "dict:" "dns:" "dtn:" "h323:" "im:" "info:" "ipp:"
251 "iris.beep:" "mtqp:" "mupdate:" "pres:" "sips:" "snmp:" "tag:"
252 "tftp:" "xmlrpc.beep:" "xmlrpc.beeps:" "xmpp:"
254 "snews:" "irc:" "mms://" "mmsh://")
255 "Uniform Resource Identifier (URI) Schemes.")
257 (defvar thing-at-point-url-regexp
258 (concat "\\<\\(" (mapconcat 'identity thing-at-point-uri-schemes
"\\|") "\\)"
259 thing-at-point-url-path-regexp
)
260 "A regular expression probably matching a complete URL.")
262 (defvar thing-at-point-markedup-url-regexp
264 "A regular expression matching a URL marked up per RFC1738.
265 This may contain whitespace (including newlines) .")
267 (put 'url
'bounds-of-thing-at-point
'thing-at-point-bounds-of-url-at-point
)
268 (defun thing-at-point-bounds-of-url-at-point ()
269 (let ((strip (thing-at-point-looking-at
270 thing-at-point-markedup-url-regexp
))) ;; (url "") short
272 (thing-at-point-looking-at thing-at-point-url-regexp
)
273 ;; Access scheme omitted?
274 ;; (setq short (thing-at-point-looking-at
275 ;; thing-at-point-short-url-regexp))
277 (let ((beginning (match-beginning 0))
280 (setq beginning
(+ beginning
5))
281 (setq end
(- end
1)))
282 (cons beginning end
)))))
284 (put 'url
'thing-at-point
'thing-at-point-url-at-point
)
285 (defun thing-at-point-url-at-point ()
286 "Return the URL around or before point.
288 Search backwards for the start of a URL ending at or after point. If
289 no URL found, return nil. The access scheme will be prepended if
290 absent: \"mailto:\" if the string contains \"@\", \"ftp://\" if it
291 starts with \"ftp\" and not \"ftp:/\", or \"http://\" by default."
293 (let ((url "") short strip
)
294 (if (or (setq strip
(thing-at-point-looking-at
295 thing-at-point-markedup-url-regexp
))
296 (thing-at-point-looking-at thing-at-point-url-regexp
)
297 ;; Access scheme omitted?
298 (setq short
(thing-at-point-looking-at
299 thing-at-point-short-url-regexp
)))
301 (setq url
(buffer-substring-no-properties (match-beginning 0)
303 (and strip
(setq url
(substring url
5 -
1))) ; Drop "<URL:" & ">"
305 (while (string-match "[ \t\n\r]+" url
)
306 (setq url
(replace-match "" t t url
)))
307 (and short
(setq url
(concat (cond ((string-match "^[a-zA-Z]+:" url
)
308 ;; already has a URL scheme.
310 ((string-match "@" url
)
312 ;; e.g. ftp.swiss... or ftp-swiss...
313 ((string-match "^ftp" url
)
317 (if (string-equal "" url
)
321 ;; The normal thingatpt mechanism doesn't work for complex regexps.
322 ;; This should work for almost any regexp wherever we are in the
323 ;; match. To do a perfect job for any arbitrary regexp would mean
324 ;; testing every position before point. Regexp searches won't find
325 ;; matches that straddle the start position so we search forwards once
326 ;; and then back repeatedly and then back up a char at a time.
328 (defun thing-at-point-looking-at (regexp)
329 "Return non-nil if point is in or just after a match for REGEXP.
330 Set the match data from the earliest such match ending at or after
333 (let ((old-point (point)) match
)
334 (and (looking-at regexp
)
335 (>= (match-end 0) old-point
)
336 (setq match
(point)))
337 ;; Search back repeatedly from end of next match.
338 ;; This may fail if next match ends before this match does.
339 (re-search-forward regexp nil
'limit
)
340 (while (and (re-search-backward regexp nil t
)
341 (or (> (match-beginning 0) old-point
)
342 (and (looking-at regexp
) ; Extend match-end past search start
343 (>= (match-end 0) old-point
)
344 (setq match
(point))))))
347 ;; Back up a char at a time in case search skipped
348 ;; intermediate match straddling search start pos.
349 (while (and (not (bobp))
350 (progn (backward-char 1) (looking-at regexp
))
351 (>= (match-end 0) old-point
)
352 (setq match
(point))))
354 (looking-at regexp
)))))
358 (let ((bounds (thing-at-point-bounds-of-url-at-point)))
360 (goto-char (cdr bounds
))
361 (error "No URL here")))))
362 (put 'url
'beginning-op
364 (let ((bounds (thing-at-point-bounds-of-url-at-point)))
366 (goto-char (car bounds
))
367 (error "No URL here")))))
370 (defvar thing-at-point-email-regexp
371 "<?[-+_.~a-zA-Z][-+_.~:a-zA-Z0-9]*@[-.a-zA-Z0-9]+>?"
372 "A regular expression probably matching an email address.
373 This does not match the real name portion, only the address, optionally
374 with angle brackets.")
376 ;; Haven't set 'forward-op on 'email nor defined 'forward-email' because
377 ;; not sure they're actually needed, and URL seems to skip them too.
378 ;; Note that (end-of-thing 'email) and (beginning-of-thing 'email)
379 ;; work automagically, though.
381 (put 'email
'bounds-of-thing-at-point
383 (let ((thing (thing-at-point-looking-at thing-at-point-email-regexp
)))
385 (let ((beginning (match-beginning 0))
387 (cons beginning end
))))))
389 (put 'email
'thing-at-point
391 (let ((boundary-pair (bounds-of-thing-at-point 'email
)))
393 (buffer-substring-no-properties
394 (car boundary-pair
) (cdr boundary-pair
))))))
398 (defun forward-whitespace (arg)
401 (re-search-forward "[ \t]+\\|\n" nil
'move arg
)
403 (if (re-search-backward "[ \t]+\\|\n" nil
'move
)
404 (or (eq (char-after (match-beginning 0)) 10)
405 (skip-chars-backward " \t")))
406 (setq arg
(1+ arg
)))))
410 (put 'buffer
'end-op
(lambda () (goto-char (point-max))))
411 (put 'buffer
'beginning-op
(lambda () (goto-char (point-min))))
415 (defun forward-symbol (arg)
418 (re-search-forward "\\(\\sw\\|\\s_\\)+" nil
'move arg
)
420 (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil
'move
)
421 (skip-syntax-backward "w_"))
422 (setq arg
(1+ arg
)))))
426 (defun forward-same-syntax (&optional arg
)
429 (skip-syntax-backward
430 (char-to-string (char-syntax (char-before))))
433 (skip-syntax-forward (char-to-string (char-syntax (char-after))))
434 (setq arg
(1- arg
))))
438 (defun word-at-point () (thing-at-point 'word
))
439 (defun sentence-at-point () (thing-at-point 'sentence
))
441 (defun read-from-whole-string (str)
442 "Read a Lisp expression from STR.
443 Signal an error if the entire string was not used."
444 (let* ((read-data (read-from-string str
))
447 ;; The call to `ignore' suppresses a compiler warning.
448 (progn (ignore (read-from-string (substring str
(cdr read-data
))))
452 (error "Can't read whole string")
455 (defun form-at-point (&optional thing pred
)
456 (let ((sexp (condition-case nil
457 (read-from-whole-string (thing-at-point (or thing
'sexp
)))
459 (if (or (not pred
) (funcall pred sexp
)) sexp
)))
462 (defun sexp-at-point ()
463 "Return the sexp at point, or nil if none is found."
464 (form-at-point 'sexp
))
466 (defun symbol-at-point ()
467 "Return the symbol at point, or nil if none is found."
468 (let ((thing (thing-at-point 'symbol
)))
469 (if thing
(intern thing
))))
471 (defun number-at-point ()
472 "Return the number at point, or nil if none is found."
473 (form-at-point 'sexp
'numberp
))
475 (defun list-at-point ()
476 "Return the Lisp list at point, or nil if none is found."
477 (form-at-point 'list
'listp
))
479 ;;; thingatpt.el ends here