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, 2010
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
'bounds-of-thing-at-point
'thing-at-point-bounds-of-list-at-point
)
193 (defun thing-at-point-bounds-of-list-at-point ()
195 (let ((opoint (point))
196 (beg (condition-case nil
202 (progn (forward-sexp)
204 ;; Are we are at the beginning of a top-level sexp?
208 (if (>= opoint
(point))
212 ;; Filenames and URLs www.com/foo%32bar
214 (defvar thing-at-point-file-name-chars
"-~/[:alnum:]_.${}#%,:"
215 "Characters allowable in filenames.")
217 (put 'filename
'end-op
219 (re-search-forward (concat "\\=[" thing-at-point-file-name-chars
"]*")
221 (put 'filename
'beginning-op
223 (if (re-search-backward (concat "[^" thing-at-point-file-name-chars
"]")
226 (goto-char (point-min)))))
228 (defvar thing-at-point-url-path-regexp
229 "[^]\t\n \"'<>[^`{}]*[^]\t\n \"'<>[^`{}.,;]+"
230 "A regular expression probably matching the host and filename or e-mail part of a URL.")
232 (defvar thing-at-point-short-url-regexp
233 (concat "[-A-Za-z0-9.]+" thing-at-point-url-path-regexp
)
234 "A regular expression probably matching a URL without an access scheme.
235 Hostname matching is stricter in this case than for
236 ``thing-at-point-url-regexp''.")
238 (defvar thing-at-point-uri-schemes
239 ;; Officials from http://www.iana.org/assignments/uri-schemes.html
240 '("ftp://" "http://" "gopher://" "mailto:" "news:" "nntp:"
241 "telnet://" "wais://" "file:/" "prospero:" "z39.50s:" "z39.50r:"
242 "cid:" "mid:" "vemmi:" "service:" "imap:" "nfs:" "acap:" "rtsp:"
243 "tip:" "pop:" "data:" "dav:" "opaquelocktoken:" "sip:" "tel:" "fax:"
244 "modem:" "ldap:" "https://" "soap.beep:" "soap.beeps:" "urn:" "go:"
245 "afs:" "tn3270:" "mailserver:"
246 "crid:" "dict:" "dns:" "dtn:" "h323:" "im:" "info:" "ipp:"
247 "iris.beep:" "mtqp:" "mupdate:" "pres:" "sips:" "snmp:" "tag:"
248 "tftp:" "xmlrpc.beep:" "xmlrpc.beeps:" "xmpp:"
250 "snews:" "irc:" "mms://" "mmsh://")
251 "Uniform Resource Identifier (URI) Schemes.")
253 (defvar thing-at-point-url-regexp
254 (concat "\\<\\(" (mapconcat 'identity thing-at-point-uri-schemes
"\\|") "\\)"
255 thing-at-point-url-path-regexp
)
256 "A regular expression probably matching a complete URL.")
258 (defvar thing-at-point-markedup-url-regexp
260 "A regular expression matching a URL marked up per RFC1738.
261 This may contain whitespace (including newlines) .")
263 (put 'url
'bounds-of-thing-at-point
'thing-at-point-bounds-of-url-at-point
)
264 (defun thing-at-point-bounds-of-url-at-point ()
265 (let ((strip (thing-at-point-looking-at
266 thing-at-point-markedup-url-regexp
))) ;; (url "") short
268 (thing-at-point-looking-at thing-at-point-url-regexp
)
269 ;; Access scheme omitted?
270 ;; (setq short (thing-at-point-looking-at
271 ;; thing-at-point-short-url-regexp))
273 (let ((beginning (match-beginning 0))
276 (setq beginning
(+ beginning
5))
277 (setq end
(- end
1)))
278 (cons beginning end
)))))
280 (put 'url
'thing-at-point
'thing-at-point-url-at-point
)
281 (defun thing-at-point-url-at-point ()
282 "Return the URL around or before point.
284 Search backwards for the start of a URL ending at or after point. If
285 no URL found, return nil. The access scheme will be prepended if
286 absent: \"mailto:\" if the string contains \"@\", \"ftp://\" if it
287 starts with \"ftp\" and not \"ftp:/\", or \"http://\" by default."
289 (let ((url "") short strip
)
290 (if (or (setq strip
(thing-at-point-looking-at
291 thing-at-point-markedup-url-regexp
))
292 (thing-at-point-looking-at thing-at-point-url-regexp
)
293 ;; Access scheme omitted?
294 (setq short
(thing-at-point-looking-at
295 thing-at-point-short-url-regexp
)))
297 (setq url
(buffer-substring-no-properties (match-beginning 0)
299 (and strip
(setq url
(substring url
5 -
1))) ; Drop "<URL:" & ">"
301 (while (string-match "[ \t\n\r]+" url
)
302 (setq url
(replace-match "" t t url
)))
303 (and short
(setq url
(concat (cond ((string-match "^[a-zA-Z]+:" url
)
304 ;; already has a URL scheme.
306 ((string-match "@" url
)
308 ;; e.g. ftp.swiss... or ftp-swiss...
309 ((string-match "^ftp" url
)
313 (if (string-equal "" url
)
317 ;; The normal thingatpt mechanism doesn't work for complex regexps.
318 ;; This should work for almost any regexp wherever we are in the
319 ;; match. To do a perfect job for any arbitrary regexp would mean
320 ;; testing every position before point. Regexp searches won't find
321 ;; matches that straddle the start position so we search forwards once
322 ;; and then back repeatedly and then back up a char at a time.
324 (defun thing-at-point-looking-at (regexp)
325 "Return non-nil if point is in or just after a match for REGEXP.
326 Set the match data from the earliest such match ending at or after
329 (let ((old-point (point)) match
)
330 (and (looking-at regexp
)
331 (>= (match-end 0) old-point
)
332 (setq match
(point)))
333 ;; Search back repeatedly from end of next match.
334 ;; This may fail if next match ends before this match does.
335 (re-search-forward regexp nil
'limit
)
336 (while (and (re-search-backward regexp nil t
)
337 (or (> (match-beginning 0) old-point
)
338 (and (looking-at regexp
) ; Extend match-end past search start
339 (>= (match-end 0) old-point
)
340 (setq match
(point))))))
343 ;; Back up a char at a time in case search skipped
344 ;; intermediate match straddling search start pos.
345 (while (and (not (bobp))
346 (progn (backward-char 1) (looking-at regexp
))
347 (>= (match-end 0) old-point
)
348 (setq match
(point))))
350 (looking-at regexp
)))))
354 (let ((bounds (thing-at-point-bounds-of-url-at-point)))
356 (goto-char (cdr bounds
))
357 (error "No URL here")))))
358 (put 'url
'beginning-op
360 (let ((bounds (thing-at-point-bounds-of-url-at-point)))
362 (goto-char (car bounds
))
363 (error "No URL here")))))
366 (defvar thing-at-point-email-regexp
367 "<?[-+_.~a-zA-Z][-+_.~:a-zA-Z0-9]*@[-.a-zA-Z0-9]+>?"
368 "A regular expression probably matching an email address.
369 This does not match the real name portion, only the address, optionally
370 with angle brackets.")
372 ;; Haven't set 'forward-op on 'email nor defined 'forward-email' because
373 ;; not sure they're actually needed, and URL seems to skip them too.
374 ;; Note that (end-of-thing 'email) and (beginning-of-thing 'email)
375 ;; work automagically, though.
377 (put 'email
'bounds-of-thing-at-point
379 (let ((thing (thing-at-point-looking-at thing-at-point-email-regexp
)))
381 (let ((beginning (match-beginning 0))
383 (cons beginning end
))))))
385 (put 'email
'thing-at-point
387 (let ((boundary-pair (bounds-of-thing-at-point 'email
)))
389 (buffer-substring-no-properties
390 (car boundary-pair
) (cdr boundary-pair
))))))
394 (defun forward-whitespace (arg)
397 (re-search-forward "[ \t]+\\|\n" nil
'move arg
)
399 (if (re-search-backward "[ \t]+\\|\n" nil
'move
)
400 (or (eq (char-after (match-beginning 0)) 10)
401 (skip-chars-backward " \t")))
402 (setq arg
(1+ arg
)))))
406 (put 'buffer
'end-op
(lambda () (goto-char (point-max))))
407 (put 'buffer
'beginning-op
(lambda () (goto-char (point-min))))
411 (defun forward-symbol (arg)
414 (re-search-forward "\\(\\sw\\|\\s_\\)+" nil
'move arg
)
416 (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil
'move
)
417 (skip-syntax-backward "w_"))
418 (setq arg
(1+ arg
)))))
422 (defun forward-same-syntax (&optional arg
)
425 (skip-syntax-backward
426 (char-to-string (char-syntax (char-before))))
429 (skip-syntax-forward (char-to-string (char-syntax (char-after))))
430 (setq arg
(1- arg
))))
434 (defun word-at-point () (thing-at-point 'word
))
435 (defun sentence-at-point () (thing-at-point 'sentence
))
437 (defun read-from-whole-string (str)
438 "Read a Lisp expression from STR.
439 Signal an error if the entire string was not used."
440 (let* ((read-data (read-from-string str
))
443 ;; The call to `ignore' suppresses a compiler warning.
444 (progn (ignore (read-from-string (substring str
(cdr read-data
))))
448 (error "Can't read whole string")
451 (defun form-at-point (&optional thing pred
)
452 (let ((sexp (condition-case nil
453 (read-from-whole-string (thing-at-point (or thing
'sexp
)))
455 (if (or (not pred
) (funcall pred sexp
)) sexp
)))
458 (defun sexp-at-point ()
459 "Return the sexp at point, or nil if none is found."
460 (form-at-point 'sexp
))
462 (defun symbol-at-point ()
463 "Return the symbol at point, or nil if none is found."
464 (let ((thing (thing-at-point 'symbol
)))
465 (if thing
(intern thing
))))
467 (defun number-at-point ()
468 "Return the number at point, or nil if none is found."
469 (form-at-point 'sexp
'numberp
))
471 (defun list-at-point ()
472 "Return the Lisp list at point, or nil if none is found."
473 (form-at-point 'list
'listp
))
475 ;; arch-tag: bb65a163-dae2-4055-aedc-fe11f497f698
476 ;;; thingatpt.el ends here