bytecomp.el fix for bug#8647
[bpt/emacs.git] / lisp / thingatpt.el
CommitLineData
55535639 1;;; thingatpt.el --- get the `thing' at point
1a2b6c52 2
95df8112 3;; Copyright (C) 1991-1998, 2000-2011 Free Software Foundation, Inc.
1a2b6c52
RS
4
5;; Author: Mike Williams <mikew@gopher.dosli.govt.nz>
6254fc9f 6;; Maintainer: FSF
b7f66977 7;; Keywords: extensions, matching, mouse
1a2b6c52 8;; Created: Thu Mar 28 13:48:23 1991
1a2b6c52
RS
9
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
1a2b6c52 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
1a2b6c52
RS
16
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.
21
eb3fa2cf
GM
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/>.
24
69f9ba7e 25;;; Commentary:
b578f267 26
c851323f
RS
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
7a8f27db 29;; its beginning and end positions in the buffer.
1a2b6c52
RS
30;;
31;; The function bounds-of-thing-at-point finds the beginning and end
c851323f 32;; positions by moving first forward to the end of the "thing", and then
1a2b6c52 33;; backwards to the beginning. By default, it uses the corresponding
c851323f 34;; forward-"thing" operator (eg. forward-word, forward-line).
1a2b6c52
RS
35;;
36;; Special cases are allowed for using properties associated with the named
f1180544 37;; "thing":
1a2b6c52 38;;
c851323f 39;; forward-op Function to call to skip forward over a "thing" (or
1a2b6c52 40;; with a negative argument, backward).
f1180544 41;;
c851323f
RS
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".
1a2b6c52
RS
44;;
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)
49
b578f267 50;;; Code:
1a2b6c52
RS
51
52(provide 'thingatpt)
53
b578f267 54;; Basic movement
1a2b6c52
RS
55
56;;;###autoload
c851323f 57(defun forward-thing (thing &optional n)
2a59b30d 58 "Move forward to the end of the Nth next THING."
c851323f
RS
59 (let ((forward-op (or (get thing 'forward-op)
60 (intern-soft (format "forward-%s" thing)))))
6254fc9f 61 (if (functionp forward-op)
c851323f
RS
62 (funcall forward-op (or n 1))
63 (error "Can't determine how to move over a %s" thing))))
1a2b6c52 64
b578f267 65;; General routines
1a2b6c52
RS
66
67;;;###autoload
c851323f
RS
68(defun bounds-of-thing-at-point (thing)
69 "Determine the start and end buffer locations for the THING at point.
70THING is a symbol which specifies the kind of syntactic entity you want.
71Possibilities include `symbol', `list', `sexp', `defun', `filename', `url',
baef4cbe 72`email', `word', `sentence', `whitespace', `line', `page' and others.
c851323f
RS
73
74See the file `thingatpt.el' for documentation on how to define
75a symbol as a valid THING.
76
77The value is a cons cell (START . END) giving the start and end positions
78of the textual entity that was found."
d9cc804b
RS
79 (if (get thing 'bounds-of-thing-at-point)
80 (funcall (get thing 'bounds-of-thing-at-point))
81 (let ((orig (point)))
82 (condition-case nil
83 (save-excursion
84 ;; Try moving forward, then back.
2a59b30d
SM
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))))
91 (let ((beg (point)))
d9cc804b
RS
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.
96 (let ((real-end
f1180544
JB
97 (progn
98 (funcall
99 (or (get thing 'end-op)
2a59b30d 100 (lambda () (forward-thing thing 1))))
d9cc804b
RS
101 (point))))
102 (if (and beg real-end (<= beg orig) (<= orig real-end))
103 (cons beg real-end)))
104 (goto-char orig)
105 ;; Try a second time, moving backward first and then forward,
106 ;; so that we can find a thing that ends at ORIG.
2a59b30d
SM
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))))
113 (let ((end (point))
114 (real-beg
f1180544
JB
115 (progn
116 (funcall
117 (or (get thing 'beginning-op)
2a59b30d 118 (lambda () (forward-thing thing -1))))
d9cc804b
RS
119 (point))))
120 (if (and real-beg end (<= real-beg orig) (<= orig end))
121 (cons real-beg end))))))
122 (error nil)))))
1a2b6c52
RS
123
124;;;###autoload
c851323f
RS
125(defun thing-at-point (thing)
126 "Return the THING at point.
127THING is a symbol which specifies the kind of syntactic entity you want.
128Possibilities include `symbol', `list', `sexp', `defun', `filename', `url',
baef4cbe 129`email', `word', `sentence', `whitespace', `line', `page' and others.
c851323f
RS
130
131See the file `thingatpt.el' for documentation on how to define
132a symbol as a valid THING."
d9cc804b
RS
133 (if (get thing 'thing-at-point)
134 (funcall (get thing 'thing-at-point))
135 (let ((bounds (bounds-of-thing-at-point thing)))
f1180544 136 (if bounds
d9cc804b 137 (buffer-substring (car bounds) (cdr bounds))))))
1a2b6c52 138
b578f267 139;; Go to beginning/end
1a2b6c52 140
c851323f
RS
141(defun beginning-of-thing (thing)
142 (let ((bounds (bounds-of-thing-at-point thing)))
143 (or bounds (error "No %s here" thing))
1a2b6c52
RS
144 (goto-char (car bounds))))
145
c851323f
RS
146(defun end-of-thing (thing)
147 (let ((bounds (bounds-of-thing-at-point thing)))
148 (or bounds (error "No %s here" thing))
1a2b6c52
RS
149 (goto-char (cdr bounds))))
150
f1180544 151;; Special cases
1a2b6c52 152
f1180544 153;; Lines
9f5c7ace
RS
154
155;; bolp will be false when you click on the last line in the buffer
156;; and it has no final newline.
157
158(put 'line 'beginning-op
2a59b30d 159 (lambda () (if (bolp) (forward-line -1) (beginning-of-line))))
9f5c7ace 160
f1180544 161;; Sexps
1a2b6c52
RS
162
163(defun in-string-p ()
164 (let ((orig (point)))
165 (save-excursion
166 (beginning-of-defun)
167 (nth 3 (parse-partial-sexp (point) orig)))))
168
169(defun end-of-sexp ()
ce8fb8aa 170 (let ((char-syntax (char-syntax (char-after))))
1a2b6c52
RS
171 (if (or (eq char-syntax ?\))
172 (and (eq char-syntax ?\") (in-string-p)))
173 (forward-char 1)
174 (forward-sexp 1))))
175
176(put 'sexp 'end-op 'end-of-sexp)
177
6f0e09d4 178(defun beginning-of-sexp ()
ce8fb8aa 179 (let ((char-syntax (char-syntax (char-before))))
6f0e09d4
RS
180 (if (or (eq char-syntax ?\()
181 (and (eq char-syntax ?\") (in-string-p)))
182 (forward-char -1)
183 (forward-sexp -1))))
184
185(put 'sexp 'beginning-op 'beginning-of-sexp)
186
f1180544 187;; Lists
1a2b6c52 188
965b9376
CY
189(put 'list 'bounds-of-thing-at-point 'thing-at-point-bounds-of-list-at-point)
190
191(defun thing-at-point-bounds-of-list-at-point ()
192 (save-excursion
193 (let ((opoint (point))
194 (beg (condition-case nil
195 (progn (up-list -1)
196 (point))
197 (error nil))))
198 (condition-case nil
199 (if beg
200 (progn (forward-sexp)
201 (cons beg (point)))
202 ;; Are we are at the beginning of a top-level sexp?
203 (forward-sexp)
204 (let ((end (point)))
205 (backward-sexp)
206 (if (>= opoint (point))
207 (cons opoint end))))
208 (error nil)))))
1a2b6c52 209
e8974c48
DA
210;; Defuns
211
212(put 'defun 'beginning-op 'beginning-of-defun)
213(put 'defun 'end-op 'end-of-defun)
214(put 'defun 'forward-op 'end-of-defun)
215
4d61e7d5 216;; Filenames and URLs www.com/foo%32bar
1a2b6c52 217
839aacc9 218(defvar thing-at-point-file-name-chars "-~/[:alnum:]_.${}#%,:"
1a2b6c52
RS
219 "Characters allowable in filenames.")
220
f1180544 221(put 'filename 'end-op
839aacc9
DL
222 (lambda ()
223 (re-search-forward (concat "\\=[" thing-at-point-file-name-chars "]*")
224 nil t)))
1a2b6c52 225(put 'filename 'beginning-op
839aacc9
DL
226 (lambda ()
227 (if (re-search-backward (concat "[^" thing-at-point-file-name-chars "]")
228 nil t)
229 (forward-char)
230 (goto-char (point-min)))))
c851323f 231
d9cc804b 232(defvar thing-at-point-url-path-regexp
42986283 233 "[^]\t\n \"'<>[^`{}]*[^]\t\n \"'<>[^`{}.,;]+"
0408f074 234 "A regular expression probably matching the host and filename or e-mail part of a URL.")
d9cc804b
RS
235
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.
239Hostname matching is stricter in this case than for
240``thing-at-point-url-regexp''.")
241
1c1766c7 242(defvar thing-at-point-uri-schemes
4f5d4668 243 ;; Officials from http://www.iana.org/assignments/uri-schemes.html
1c1766c7
MR
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:"
4f5d4668
RS
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:"
1c1766c7 253 ;; Compatibility
4f5d4668 254 "snews:" "irc:" "mms://" "mmsh://")
2a59b30d 255 "Uniform Resource Identifier (URI) Schemes.")
1c1766c7 256
d9cc804b 257(defvar thing-at-point-url-regexp
1c1766c7
MR
258 (concat "\\<\\(" (mapconcat 'identity thing-at-point-uri-schemes "\\|") "\\)"
259 thing-at-point-url-path-regexp)
d9cc804b
RS
260 "A regular expression probably matching a complete URL.")
261
262(defvar thing-at-point-markedup-url-regexp
263 "<URL:[^>]+>"
264 "A regular expression matching a URL marked up per RFC1738.
265This may contain whitespace (including newlines) .")
266
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 ()
2a59b30d
SM
269 (let ((strip (thing-at-point-looking-at
270 thing-at-point-markedup-url-regexp))) ;; (url "") short
271 (if (or strip
883d1f4b 272 (thing-at-point-looking-at thing-at-point-url-regexp)
d9cc804b 273 ;; Access scheme omitted?
2a59b30d
SM
274 ;; (setq short (thing-at-point-looking-at
275 ;; thing-at-point-short-url-regexp))
276 )
d9cc804b
RS
277 (let ((beginning (match-beginning 0))
278 (end (match-end 0)))
2a59b30d
SM
279 (when strip
280 (setq beginning (+ beginning 5))
281 (setq end (- end 1)))
d9cc804b
RS
282 (cons beginning end)))))
283
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.
340483df
DL
287
288Search backwards for the start of a URL ending at or after point. If
289no URL found, return nil. The access scheme will be prepended if
290absent: \"mailto:\" if the string contains \"@\", \"ftp://\" if it
291starts with \"ftp\" and not \"ftp:/\", or \"http://\" by default."
292
d9cc804b
RS
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)))
300 (progn
301 (setq url (buffer-substring-no-properties (match-beginning 0)
302 (match-end 0)))
303 (and strip (setq url (substring url 5 -1))) ; Drop "<URL:" & ">"
304 ;; strip whitespace
84841dd1 305 (while (string-match "[ \t\n\r]+" url)
d9cc804b 306 (setq url (replace-match "" t t url)))
4f5d4668
RS
307 (and short (setq url (concat (cond ((string-match "^[a-zA-Z]+:" url)
308 ;; already has a URL scheme.
309 "")
310 ((string-match "@" url)
340483df
DL
311 "mailto:")
312 ;; e.g. ftp.swiss... or ftp-swiss...
313 ((string-match "^ftp" url)
314 "ftp://")
315 (t "http://"))
316 url)))
d9cc804b
RS
317 (if (string-equal "" url)
318 nil
319 url)))))
320
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.
327
328(defun thing-at-point-looking-at (regexp)
329 "Return non-nil if point is in or just after a match for REGEXP.
330Set the match data from the earliest such match ending at or after
331point."
332 (save-excursion
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))))))
345 (if (not match) nil
346 (goto-char match)
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))))
353 (goto-char match)
354 (looking-at regexp)))))
355
a1c9b4d0 356(put 'url 'end-op
2a59b30d
SM
357 (lambda ()
358 (let ((bounds (thing-at-point-bounds-of-url-at-point)))
359 (if bounds
360 (goto-char (cdr bounds))
361 (error "No URL here")))))
c851323f 362(put 'url 'beginning-op
2a59b30d
SM
363 (lambda ()
364 (let ((bounds (thing-at-point-bounds-of-url-at-point)))
365 (if bounds
366 (goto-char (car bounds))
367 (error "No URL here")))))
1a2b6c52 368
baef4cbe
KF
369;; Email addresses
370(defvar thing-at-point-email-regexp
86265518 371 "<?[-+_.~a-zA-Z][-+_.~:a-zA-Z0-9]*@[-.a-zA-Z0-9]+>?"
baef4cbe
KF
372 "A regular expression probably matching an email address.
373This does not match the real name portion, only the address, optionally
374with angle brackets.")
375
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.
380
381(put 'email 'bounds-of-thing-at-point
382 (lambda ()
383 (let ((thing (thing-at-point-looking-at thing-at-point-email-regexp)))
384 (if thing
385 (let ((beginning (match-beginning 0))
386 (end (match-end 0)))
387 (cons beginning end))))))
388
389(put 'email 'thing-at-point
390 (lambda ()
391 (let ((boundary-pair (bounds-of-thing-at-point 'email)))
392 (if boundary-pair
393 (buffer-substring-no-properties
394 (car boundary-pair) (cdr boundary-pair))))))
395
f1180544 396;; Whitespace
1a2b6c52 397
c851323f 398(defun forward-whitespace (arg)
1a2b6c52 399 (interactive "p")
f1180544 400 (if (natnump arg)
9e594a2e 401 (re-search-forward "[ \t]+\\|\n" nil 'move arg)
c851323f 402 (while (< arg 0)
9e594a2e 403 (if (re-search-backward "[ \t]+\\|\n" nil 'move)
1a2b6c52
RS
404 (or (eq (char-after (match-beginning 0)) 10)
405 (skip-chars-backward " \t")))
c851323f 406 (setq arg (1+ arg)))))
1a2b6c52 407
f1180544 408;; Buffer
1a2b6c52 409
206eef6c
SM
410(put 'buffer 'end-op (lambda () (goto-char (point-max))))
411(put 'buffer 'beginning-op (lambda () (goto-char (point-min))))
1a2b6c52 412
f1180544 413;; Symbols
1a2b6c52 414
c851323f 415(defun forward-symbol (arg)
1a2b6c52 416 (interactive "p")
f1180544 417 (if (natnump arg)
9e594a2e 418 (re-search-forward "\\(\\sw\\|\\s_\\)+" nil 'move arg)
c851323f 419 (while (< arg 0)
9e594a2e 420 (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil 'move)
1a2b6c52 421 (skip-syntax-backward "w_"))
c851323f 422 (setq arg (1+ arg)))))
1a2b6c52 423
f1180544 424;; Syntax blocks
fde7326e
RS
425
426(defun forward-same-syntax (&optional arg)
427 (interactive "p")
428 (while (< arg 0)
f1180544 429 (skip-syntax-backward
ce8fb8aa 430 (char-to-string (char-syntax (char-before))))
fde7326e
RS
431 (setq arg (1+ arg)))
432 (while (> arg 0)
ce8fb8aa 433 (skip-syntax-forward (char-to-string (char-syntax (char-after))))
fde7326e
RS
434 (setq arg (1- arg))))
435
f1180544 436;; Aliases
1a2b6c52
RS
437
438(defun word-at-point () (thing-at-point 'word))
439(defun sentence-at-point () (thing-at-point 'sentence))
440
c851323f 441(defun read-from-whole-string (str)
2a59b30d 442 "Read a Lisp expression from STR.
c851323f
RS
443Signal an error if the entire string was not used."
444 (let* ((read-data (read-from-string str))
f1180544 445 (more-left
1a2b6c52 446 (condition-case nil
1b0d40de 447 ;; The call to `ignore' suppresses a compiler warning.
7f1422bc 448 (progn (ignore (read-from-string (substring str (cdr read-data))))
1a2b6c52
RS
449 t)
450 (end-of-file nil))))
451 (if more-left
452 (error "Can't read whole string")
453 (car read-data))))
454
f1180544
JB
455(defun form-at-point (&optional thing pred)
456 (let ((sexp (condition-case nil
c851323f 457 (read-from-whole-string (thing-at-point (or thing 'sexp)))
1a2b6c52 458 (error nil))))
c851323f 459 (if (or (not pred) (funcall pred sexp)) sexp)))
1a2b6c52 460
be64abcf 461;;;###autoload
de285f27
CY
462(defun sexp-at-point ()
463 "Return the sexp at point, or nil if none is found."
464 (form-at-point 'sexp))
be64abcf 465;;;###autoload
2a59b30d 466(defun symbol-at-point ()
de285f27 467 "Return the symbol at point, or nil if none is found."
2a59b30d
SM
468 (let ((thing (thing-at-point 'symbol)))
469 (if thing (intern thing))))
be64abcf 470;;;###autoload
de285f27
CY
471(defun number-at-point ()
472 "Return the number at point, or nil if none is found."
473 (form-at-point 'sexp 'numberp))
be64abcf 474;;;###autoload
de285f27
CY
475(defun list-at-point ()
476 "Return the Lisp list at point, or nil if none is found."
477 (form-at-point 'list 'listp))
1a2b6c52 478
55535639 479;;; thingatpt.el ends here