1 ;;; thingatpt.el --- get the `thing' at point
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 ;; Free Software Foundation, Inc.
7 ;; Author: Mike Williams <mikew@gopher.dosli.govt.nz>
9 ;; Keywords: extensions, matching, mouse
10 ;; Created: Thu Mar 28 13:48:23 1991
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; This file provides routines for getting the "thing" at the location of
30 ;; point, whatever that "thing" happens to be. The "thing" is defined by
31 ;; its beginning and end positions in the buffer.
33 ;; The function bounds-of-thing-at-point finds the beginning and end
34 ;; positions by moving first forward to the end of the "thing", and then
35 ;; backwards to the beginning. By default, it uses the corresponding
36 ;; forward-"thing" operator (eg. forward-word, forward-line).
38 ;; Special cases are allowed for using properties associated with the named
41 ;; forward-op Function to call to skip forward over a "thing" (or
42 ;; with a negative argument, backward).
44 ;; beginning-op Function to call to skip to the beginning of a "thing".
45 ;; end-op Function to call to skip to the end of a "thing".
47 ;; Reliance on existing operators means that many `things' can be accessed
48 ;; without further code: eg.
49 ;; (thing-at-point 'line)
50 ;; (thing-at-point 'page)
59 (defun forward-thing (thing &optional n
)
60 "Move forward to the end of the Nth next THING."
61 (let ((forward-op (or (get thing
'forward-op
)
62 (intern-soft (format "forward-%s" thing
)))))
63 (if (functionp forward-op
)
64 (funcall forward-op
(or n
1))
65 (error "Can't determine how to move over a %s" thing
))))
70 (defun bounds-of-thing-at-point (thing)
71 "Determine the start and end buffer locations for the THING at point.
72 THING is a symbol which specifies the kind of syntactic entity you want.
73 Possibilities include `symbol', `list', `sexp', `defun', `filename', `url',
74 `email', `word', `sentence', `whitespace', `line', `page' and others.
76 See the file `thingatpt.el' for documentation on how to define
77 a symbol as a valid THING.
79 The value is a cons cell (START . END) giving the start and end positions
80 of the textual entity that was found."
81 (if (get thing
'bounds-of-thing-at-point
)
82 (funcall (get thing
'bounds-of-thing-at-point
))
86 ;; Try moving forward, then back.
87 (funcall ;; First move to end.
88 (or (get thing
'end-op
)
89 (lambda () (forward-thing thing
1))))
90 (funcall ;; Then move to beg.
91 (or (get thing
'beginning-op
)
92 (lambda () (forward-thing thing -
1))))
94 (if (not (and beg
(> beg orig
)))
95 ;; If that brings us all the way back to ORIG,
96 ;; it worked. But END may not be the real end.
97 ;; So find the real end that corresponds to BEG.
101 (or (get thing
'end-op
)
102 (lambda () (forward-thing thing
1))))
104 (if (and beg real-end
(<= beg orig
) (<= orig real-end
))
105 (cons beg real-end
)))
107 ;; Try a second time, moving backward first and then forward,
108 ;; so that we can find a thing that ends at ORIG.
109 (funcall ;; First, move to beg.
110 (or (get thing
'beginning-op
)
111 (lambda () (forward-thing thing -
1))))
112 (funcall ;; Then move to end.
113 (or (get thing
'end-op
)
114 (lambda () (forward-thing thing
1))))
119 (or (get thing
'beginning-op
)
120 (lambda () (forward-thing thing -
1))))
122 (if (and real-beg end
(<= real-beg orig
) (<= orig end
))
123 (cons real-beg end
))))))
127 (defun thing-at-point (thing)
128 "Return the THING at point.
129 THING is a symbol which specifies the kind of syntactic entity you want.
130 Possibilities include `symbol', `list', `sexp', `defun', `filename', `url',
131 `email', `word', `sentence', `whitespace', `line', `page' and others.
133 See the file `thingatpt.el' for documentation on how to define
134 a symbol as a valid THING."
135 (if (get thing
'thing-at-point
)
136 (funcall (get thing
'thing-at-point
))
137 (let ((bounds (bounds-of-thing-at-point thing
)))
139 (buffer-substring (car bounds
) (cdr bounds
))))))
141 ;; Go to beginning/end
143 (defun beginning-of-thing (thing)
144 (let ((bounds (bounds-of-thing-at-point thing
)))
145 (or bounds
(error "No %s here" thing
))
146 (goto-char (car bounds
))))
148 (defun end-of-thing (thing)
149 (let ((bounds (bounds-of-thing-at-point thing
)))
150 (or bounds
(error "No %s here" thing
))
151 (goto-char (cdr bounds
))))
157 ;; bolp will be false when you click on the last line in the buffer
158 ;; and it has no final newline.
160 (put 'line
'beginning-op
161 (lambda () (if (bolp) (forward-line -
1) (beginning-of-line))))
165 (defun in-string-p ()
166 (let ((orig (point)))
169 (nth 3 (parse-partial-sexp (point) orig
)))))
171 (defun end-of-sexp ()
172 (let ((char-syntax (char-syntax (char-after))))
173 (if (or (eq char-syntax ?\
))
174 (and (eq char-syntax ?
\") (in-string-p)))
178 (put 'sexp
'end-op
'end-of-sexp
)
180 (defun beginning-of-sexp ()
181 (let ((char-syntax (char-syntax (char-before))))
182 (if (or (eq char-syntax ?\
()
183 (and (eq char-syntax ?
\") (in-string-p)))
187 (put 'sexp
'beginning-op
'beginning-of-sexp
)
191 (put 'list
'end-op
(lambda () (up-list 1)))
192 (put 'list
'beginning-op
'backward-sexp
)
194 ;; Filenames and URLs www.com/foo%32bar
196 (defvar thing-at-point-file-name-chars
"-~/[:alnum:]_.${}#%,:"
197 "Characters allowable in filenames.")
199 (put 'filename
'end-op
201 (re-search-forward (concat "\\=[" thing-at-point-file-name-chars
"]*")
203 (put 'filename
'beginning-op
205 (if (re-search-backward (concat "[^" thing-at-point-file-name-chars
"]")
208 (goto-char (point-min)))))
210 (defvar thing-at-point-url-path-regexp
211 "[^]\t\n \"'<>[^`{}]*[^]\t\n \"'<>[^`{}.,;]+"
212 "A regular expression probably matching the host and filename or e-mail part of a URL.")
214 (defvar thing-at-point-short-url-regexp
215 (concat "[-A-Za-z0-9.]+" thing-at-point-url-path-regexp
)
216 "A regular expression probably matching a URL without an access scheme.
217 Hostname matching is stricter in this case than for
218 ``thing-at-point-url-regexp''.")
220 (defvar thing-at-point-uri-schemes
221 ;; Officials from http://www.iana.org/assignments/uri-schemes.html
222 '("ftp://" "http://" "gopher://" "mailto:" "news:" "nntp:"
223 "telnet://" "wais://" "file:/" "prospero:" "z39.50s:" "z39.50r:"
224 "cid:" "mid:" "vemmi:" "service:" "imap:" "nfs:" "acap:" "rtsp:"
225 "tip:" "pop:" "data:" "dav:" "opaquelocktoken:" "sip:" "tel:" "fax:"
226 "modem:" "ldap:" "https://" "soap.beep:" "soap.beeps:" "urn:" "go:"
227 "afs:" "tn3270:" "mailserver:"
228 "crid:" "dict:" "dns:" "dtn:" "h323:" "im:" "info:" "ipp:"
229 "iris.beep:" "mtqp:" "mupdate:" "pres:" "sips:" "snmp:" "tag:"
230 "tftp:" "xmlrpc.beep:" "xmlrpc.beeps:" "xmpp:"
232 "snews:" "irc:" "mms://" "mmsh://")
233 "Uniform Resource Identifier (URI) Schemes.")
235 (defvar thing-at-point-url-regexp
236 (concat "\\<\\(" (mapconcat 'identity thing-at-point-uri-schemes
"\\|") "\\)"
237 thing-at-point-url-path-regexp
)
238 "A regular expression probably matching a complete URL.")
240 (defvar thing-at-point-markedup-url-regexp
242 "A regular expression matching a URL marked up per RFC1738.
243 This may contain whitespace (including newlines) .")
245 (put 'url
'bounds-of-thing-at-point
'thing-at-point-bounds-of-url-at-point
)
246 (defun thing-at-point-bounds-of-url-at-point ()
247 (let ((strip (thing-at-point-looking-at
248 thing-at-point-markedup-url-regexp
))) ;; (url "") short
250 (thing-at-point-looking-at thing-at-point-url-regexp
)
251 ;; Access scheme omitted?
252 ;; (setq short (thing-at-point-looking-at
253 ;; thing-at-point-short-url-regexp))
255 (let ((beginning (match-beginning 0))
258 (setq beginning
(+ beginning
5))
259 (setq end
(- end
1)))
260 (cons beginning end
)))))
262 (put 'url
'thing-at-point
'thing-at-point-url-at-point
)
263 (defun thing-at-point-url-at-point ()
264 "Return the URL around or before point.
266 Search backwards for the start of a URL ending at or after point. If
267 no URL found, return nil. The access scheme will be prepended if
268 absent: \"mailto:\" if the string contains \"@\", \"ftp://\" if it
269 starts with \"ftp\" and not \"ftp:/\", or \"http://\" by default."
271 (let ((url "") short strip
)
272 (if (or (setq strip
(thing-at-point-looking-at
273 thing-at-point-markedup-url-regexp
))
274 (thing-at-point-looking-at thing-at-point-url-regexp
)
275 ;; Access scheme omitted?
276 (setq short
(thing-at-point-looking-at
277 thing-at-point-short-url-regexp
)))
279 (setq url
(buffer-substring-no-properties (match-beginning 0)
281 (and strip
(setq url
(substring url
5 -
1))) ; Drop "<URL:" & ">"
283 (while (string-match "[ \t\n\r]+" url
)
284 (setq url
(replace-match "" t t url
)))
285 (and short
(setq url
(concat (cond ((string-match "^[a-zA-Z]+:" url
)
286 ;; already has a URL scheme.
288 ((string-match "@" url
)
290 ;; e.g. ftp.swiss... or ftp-swiss...
291 ((string-match "^ftp" url
)
295 (if (string-equal "" url
)
299 ;; The normal thingatpt mechanism doesn't work for complex regexps.
300 ;; This should work for almost any regexp wherever we are in the
301 ;; match. To do a perfect job for any arbitrary regexp would mean
302 ;; testing every position before point. Regexp searches won't find
303 ;; matches that straddle the start position so we search forwards once
304 ;; and then back repeatedly and then back up a char at a time.
306 (defun thing-at-point-looking-at (regexp)
307 "Return non-nil if point is in or just after a match for REGEXP.
308 Set the match data from the earliest such match ending at or after
311 (let ((old-point (point)) match
)
312 (and (looking-at regexp
)
313 (>= (match-end 0) old-point
)
314 (setq match
(point)))
315 ;; Search back repeatedly from end of next match.
316 ;; This may fail if next match ends before this match does.
317 (re-search-forward regexp nil
'limit
)
318 (while (and (re-search-backward regexp nil t
)
319 (or (> (match-beginning 0) old-point
)
320 (and (looking-at regexp
) ; Extend match-end past search start
321 (>= (match-end 0) old-point
)
322 (setq match
(point))))))
325 ;; Back up a char at a time in case search skipped
326 ;; intermediate match straddling search start pos.
327 (while (and (not (bobp))
328 (progn (backward-char 1) (looking-at regexp
))
329 (>= (match-end 0) old-point
)
330 (setq match
(point))))
332 (looking-at regexp
)))))
336 (let ((bounds (thing-at-point-bounds-of-url-at-point)))
338 (goto-char (cdr bounds
))
339 (error "No URL here")))))
340 (put 'url
'beginning-op
342 (let ((bounds (thing-at-point-bounds-of-url-at-point)))
344 (goto-char (car bounds
))
345 (error "No URL here")))))
348 (defvar thing-at-point-email-regexp
349 "<?[-+_.~a-zA-Z][-+_.~:a-zA-Z0-9]*@[-.a-zA-Z0-9]+>?"
350 "A regular expression probably matching an email address.
351 This does not match the real name portion, only the address, optionally
352 with angle brackets.")
354 ;; Haven't set 'forward-op on 'email nor defined 'forward-email' because
355 ;; not sure they're actually needed, and URL seems to skip them too.
356 ;; Note that (end-of-thing 'email) and (beginning-of-thing 'email)
357 ;; work automagically, though.
359 (put 'email
'bounds-of-thing-at-point
361 (let ((thing (thing-at-point-looking-at thing-at-point-email-regexp
)))
363 (let ((beginning (match-beginning 0))
365 (cons beginning end
))))))
367 (put 'email
'thing-at-point
369 (let ((boundary-pair (bounds-of-thing-at-point 'email
)))
371 (buffer-substring-no-properties
372 (car boundary-pair
) (cdr boundary-pair
))))))
376 (defun forward-whitespace (arg)
379 (re-search-forward "[ \t]+\\|\n" nil
'move arg
)
381 (if (re-search-backward "[ \t]+\\|\n" nil
'move
)
382 (or (eq (char-after (match-beginning 0)) 10)
383 (skip-chars-backward " \t")))
384 (setq arg
(1+ arg
)))))
388 (put 'buffer
'end-op
(lambda () (goto-char (point-max))))
389 (put 'buffer
'beginning-op
(lambda () (goto-char (point-min))))
393 (defun forward-symbol (arg)
396 (re-search-forward "\\(\\sw\\|\\s_\\)+" nil
'move arg
)
398 (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil
'move
)
399 (skip-syntax-backward "w_"))
400 (setq arg
(1+ arg
)))))
404 (defun forward-same-syntax (&optional arg
)
407 (skip-syntax-backward
408 (char-to-string (char-syntax (char-before))))
411 (skip-syntax-forward (char-to-string (char-syntax (char-after))))
412 (setq arg
(1- arg
))))
416 (defun word-at-point () (thing-at-point 'word
))
417 (defun sentence-at-point () (thing-at-point 'sentence
))
419 (defun read-from-whole-string (str)
420 "Read a Lisp expression from STR.
421 Signal an error if the entire string was not used."
422 (let* ((read-data (read-from-string str
))
425 ;; The call to `ignore' suppresses a compiler warning.
426 (progn (ignore (read-from-string (substring str
(cdr read-data
))))
430 (error "Can't read whole string")
433 (defun form-at-point (&optional thing pred
)
434 (let ((sexp (condition-case nil
435 (read-from-whole-string (thing-at-point (or thing
'sexp
)))
437 (if (or (not pred
) (funcall pred sexp
)) sexp
)))
440 (defun sexp-at-point ()
441 "Return the sexp at point, or nil if none is found."
442 (form-at-point 'sexp
))
444 (defun symbol-at-point ()
445 "Return the symbol at point, or nil if none is found."
446 (let ((thing (thing-at-point 'symbol
)))
447 (if thing
(intern thing
))))
449 (defun number-at-point ()
450 "Return the number at point, or nil if none is found."
451 (form-at-point 'sexp
'numberp
))
453 (defun list-at-point ()
454 "Return the Lisp list at point, or nil if none is found."
455 (form-at-point 'list
'listp
))
457 ;; arch-tag: bb65a163-dae2-4055-aedc-fe11f497f698
458 ;;; thingatpt.el ends here