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