Refill some long/short copyright headers.
[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
4d61e7d5 210;; Filenames and URLs www.com/foo%32bar
1a2b6c52 211
839aacc9 212(defvar thing-at-point-file-name-chars "-~/[:alnum:]_.${}#%,:"
1a2b6c52
RS
213 "Characters allowable in filenames.")
214
f1180544 215(put 'filename 'end-op
839aacc9
DL
216 (lambda ()
217 (re-search-forward (concat "\\=[" thing-at-point-file-name-chars "]*")
218 nil t)))
1a2b6c52 219(put 'filename 'beginning-op
839aacc9
DL
220 (lambda ()
221 (if (re-search-backward (concat "[^" thing-at-point-file-name-chars "]")
222 nil t)
223 (forward-char)
224 (goto-char (point-min)))))
c851323f 225
d9cc804b 226(defvar thing-at-point-url-path-regexp
42986283 227 "[^]\t\n \"'<>[^`{}]*[^]\t\n \"'<>[^`{}.,;]+"
0408f074 228 "A regular expression probably matching the host and filename or e-mail part of a URL.")
d9cc804b
RS
229
230(defvar thing-at-point-short-url-regexp
231 (concat "[-A-Za-z0-9.]+" thing-at-point-url-path-regexp)
232 "A regular expression probably matching a URL without an access scheme.
233Hostname matching is stricter in this case than for
234``thing-at-point-url-regexp''.")
235
1c1766c7 236(defvar thing-at-point-uri-schemes
4f5d4668 237 ;; Officials from http://www.iana.org/assignments/uri-schemes.html
1c1766c7
MR
238 '("ftp://" "http://" "gopher://" "mailto:" "news:" "nntp:"
239 "telnet://" "wais://" "file:/" "prospero:" "z39.50s:" "z39.50r:"
240 "cid:" "mid:" "vemmi:" "service:" "imap:" "nfs:" "acap:" "rtsp:"
241 "tip:" "pop:" "data:" "dav:" "opaquelocktoken:" "sip:" "tel:" "fax:"
242 "modem:" "ldap:" "https://" "soap.beep:" "soap.beeps:" "urn:" "go:"
243 "afs:" "tn3270:" "mailserver:"
4f5d4668
RS
244 "crid:" "dict:" "dns:" "dtn:" "h323:" "im:" "info:" "ipp:"
245 "iris.beep:" "mtqp:" "mupdate:" "pres:" "sips:" "snmp:" "tag:"
246 "tftp:" "xmlrpc.beep:" "xmlrpc.beeps:" "xmpp:"
1c1766c7 247 ;; Compatibility
4f5d4668 248 "snews:" "irc:" "mms://" "mmsh://")
2a59b30d 249 "Uniform Resource Identifier (URI) Schemes.")
1c1766c7 250
d9cc804b 251(defvar thing-at-point-url-regexp
1c1766c7
MR
252 (concat "\\<\\(" (mapconcat 'identity thing-at-point-uri-schemes "\\|") "\\)"
253 thing-at-point-url-path-regexp)
d9cc804b
RS
254 "A regular expression probably matching a complete URL.")
255
256(defvar thing-at-point-markedup-url-regexp
257 "<URL:[^>]+>"
258 "A regular expression matching a URL marked up per RFC1738.
259This may contain whitespace (including newlines) .")
260
261(put 'url 'bounds-of-thing-at-point 'thing-at-point-bounds-of-url-at-point)
262(defun thing-at-point-bounds-of-url-at-point ()
2a59b30d
SM
263 (let ((strip (thing-at-point-looking-at
264 thing-at-point-markedup-url-regexp))) ;; (url "") short
265 (if (or strip
883d1f4b 266 (thing-at-point-looking-at thing-at-point-url-regexp)
d9cc804b 267 ;; Access scheme omitted?
2a59b30d
SM
268 ;; (setq short (thing-at-point-looking-at
269 ;; thing-at-point-short-url-regexp))
270 )
d9cc804b
RS
271 (let ((beginning (match-beginning 0))
272 (end (match-end 0)))
2a59b30d
SM
273 (when strip
274 (setq beginning (+ beginning 5))
275 (setq end (- end 1)))
d9cc804b
RS
276 (cons beginning end)))))
277
278(put 'url 'thing-at-point 'thing-at-point-url-at-point)
279(defun thing-at-point-url-at-point ()
280 "Return the URL around or before point.
340483df
DL
281
282Search backwards for the start of a URL ending at or after point. If
283no URL found, return nil. The access scheme will be prepended if
284absent: \"mailto:\" if the string contains \"@\", \"ftp://\" if it
285starts with \"ftp\" and not \"ftp:/\", or \"http://\" by default."
286
d9cc804b
RS
287 (let ((url "") short strip)
288 (if (or (setq strip (thing-at-point-looking-at
289 thing-at-point-markedup-url-regexp))
290 (thing-at-point-looking-at thing-at-point-url-regexp)
291 ;; Access scheme omitted?
292 (setq short (thing-at-point-looking-at
293 thing-at-point-short-url-regexp)))
294 (progn
295 (setq url (buffer-substring-no-properties (match-beginning 0)
296 (match-end 0)))
297 (and strip (setq url (substring url 5 -1))) ; Drop "<URL:" & ">"
298 ;; strip whitespace
84841dd1 299 (while (string-match "[ \t\n\r]+" url)
d9cc804b 300 (setq url (replace-match "" t t url)))
4f5d4668
RS
301 (and short (setq url (concat (cond ((string-match "^[a-zA-Z]+:" url)
302 ;; already has a URL scheme.
303 "")
304 ((string-match "@" url)
340483df
DL
305 "mailto:")
306 ;; e.g. ftp.swiss... or ftp-swiss...
307 ((string-match "^ftp" url)
308 "ftp://")
309 (t "http://"))
310 url)))
d9cc804b
RS
311 (if (string-equal "" url)
312 nil
313 url)))))
314
315;; The normal thingatpt mechanism doesn't work for complex regexps.
316;; This should work for almost any regexp wherever we are in the
317;; match. To do a perfect job for any arbitrary regexp would mean
318;; testing every position before point. Regexp searches won't find
319;; matches that straddle the start position so we search forwards once
320;; and then back repeatedly and then back up a char at a time.
321
322(defun thing-at-point-looking-at (regexp)
323 "Return non-nil if point is in or just after a match for REGEXP.
324Set the match data from the earliest such match ending at or after
325point."
326 (save-excursion
327 (let ((old-point (point)) match)
328 (and (looking-at regexp)
329 (>= (match-end 0) old-point)
330 (setq match (point)))
331 ;; Search back repeatedly from end of next match.
332 ;; This may fail if next match ends before this match does.
333 (re-search-forward regexp nil 'limit)
334 (while (and (re-search-backward regexp nil t)
335 (or (> (match-beginning 0) old-point)
336 (and (looking-at regexp) ; Extend match-end past search start
337 (>= (match-end 0) old-point)
338 (setq match (point))))))
339 (if (not match) nil
340 (goto-char match)
341 ;; Back up a char at a time in case search skipped
342 ;; intermediate match straddling search start pos.
343 (while (and (not (bobp))
344 (progn (backward-char 1) (looking-at regexp))
345 (>= (match-end 0) old-point)
346 (setq match (point))))
347 (goto-char match)
348 (looking-at regexp)))))
349
a1c9b4d0 350(put 'url 'end-op
2a59b30d
SM
351 (lambda ()
352 (let ((bounds (thing-at-point-bounds-of-url-at-point)))
353 (if bounds
354 (goto-char (cdr bounds))
355 (error "No URL here")))))
c851323f 356(put 'url 'beginning-op
2a59b30d
SM
357 (lambda ()
358 (let ((bounds (thing-at-point-bounds-of-url-at-point)))
359 (if bounds
360 (goto-char (car bounds))
361 (error "No URL here")))))
1a2b6c52 362
baef4cbe
KF
363;; Email addresses
364(defvar thing-at-point-email-regexp
86265518 365 "<?[-+_.~a-zA-Z][-+_.~:a-zA-Z0-9]*@[-.a-zA-Z0-9]+>?"
baef4cbe
KF
366 "A regular expression probably matching an email address.
367This does not match the real name portion, only the address, optionally
368with angle brackets.")
369
370;; Haven't set 'forward-op on 'email nor defined 'forward-email' because
371;; not sure they're actually needed, and URL seems to skip them too.
372;; Note that (end-of-thing 'email) and (beginning-of-thing 'email)
373;; work automagically, though.
374
375(put 'email 'bounds-of-thing-at-point
376 (lambda ()
377 (let ((thing (thing-at-point-looking-at thing-at-point-email-regexp)))
378 (if thing
379 (let ((beginning (match-beginning 0))
380 (end (match-end 0)))
381 (cons beginning end))))))
382
383(put 'email 'thing-at-point
384 (lambda ()
385 (let ((boundary-pair (bounds-of-thing-at-point 'email)))
386 (if boundary-pair
387 (buffer-substring-no-properties
388 (car boundary-pair) (cdr boundary-pair))))))
389
f1180544 390;; Whitespace
1a2b6c52 391
c851323f 392(defun forward-whitespace (arg)
1a2b6c52 393 (interactive "p")
f1180544 394 (if (natnump arg)
9e594a2e 395 (re-search-forward "[ \t]+\\|\n" nil 'move arg)
c851323f 396 (while (< arg 0)
9e594a2e 397 (if (re-search-backward "[ \t]+\\|\n" nil 'move)
1a2b6c52
RS
398 (or (eq (char-after (match-beginning 0)) 10)
399 (skip-chars-backward " \t")))
c851323f 400 (setq arg (1+ arg)))))
1a2b6c52 401
f1180544 402;; Buffer
1a2b6c52 403
206eef6c
SM
404(put 'buffer 'end-op (lambda () (goto-char (point-max))))
405(put 'buffer 'beginning-op (lambda () (goto-char (point-min))))
1a2b6c52 406
f1180544 407;; Symbols
1a2b6c52 408
c851323f 409(defun forward-symbol (arg)
1a2b6c52 410 (interactive "p")
f1180544 411 (if (natnump arg)
9e594a2e 412 (re-search-forward "\\(\\sw\\|\\s_\\)+" nil 'move arg)
c851323f 413 (while (< arg 0)
9e594a2e 414 (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil 'move)
1a2b6c52 415 (skip-syntax-backward "w_"))
c851323f 416 (setq arg (1+ arg)))))
1a2b6c52 417
f1180544 418;; Syntax blocks
fde7326e
RS
419
420(defun forward-same-syntax (&optional arg)
421 (interactive "p")
422 (while (< arg 0)
f1180544 423 (skip-syntax-backward
ce8fb8aa 424 (char-to-string (char-syntax (char-before))))
fde7326e
RS
425 (setq arg (1+ arg)))
426 (while (> arg 0)
ce8fb8aa 427 (skip-syntax-forward (char-to-string (char-syntax (char-after))))
fde7326e
RS
428 (setq arg (1- arg))))
429
f1180544 430;; Aliases
1a2b6c52
RS
431
432(defun word-at-point () (thing-at-point 'word))
433(defun sentence-at-point () (thing-at-point 'sentence))
434
c851323f 435(defun read-from-whole-string (str)
2a59b30d 436 "Read a Lisp expression from STR.
c851323f
RS
437Signal an error if the entire string was not used."
438 (let* ((read-data (read-from-string str))
f1180544 439 (more-left
1a2b6c52 440 (condition-case nil
1b0d40de 441 ;; The call to `ignore' suppresses a compiler warning.
7f1422bc 442 (progn (ignore (read-from-string (substring str (cdr read-data))))
1a2b6c52
RS
443 t)
444 (end-of-file nil))))
445 (if more-left
446 (error "Can't read whole string")
447 (car read-data))))
448
f1180544
JB
449(defun form-at-point (&optional thing pred)
450 (let ((sexp (condition-case nil
c851323f 451 (read-from-whole-string (thing-at-point (or thing 'sexp)))
1a2b6c52 452 (error nil))))
c851323f 453 (if (or (not pred) (funcall pred sexp)) sexp)))
1a2b6c52 454
be64abcf 455;;;###autoload
de285f27
CY
456(defun sexp-at-point ()
457 "Return the sexp at point, or nil if none is found."
458 (form-at-point 'sexp))
be64abcf 459;;;###autoload
2a59b30d 460(defun symbol-at-point ()
de285f27 461 "Return the symbol at point, or nil if none is found."
2a59b30d
SM
462 (let ((thing (thing-at-point 'symbol)))
463 (if thing (intern thing))))
be64abcf 464;;;###autoload
de285f27
CY
465(defun number-at-point ()
466 "Return the number at point, or nil if none is found."
467 (form-at-point 'sexp 'numberp))
be64abcf 468;;;###autoload
de285f27
CY
469(defun list-at-point ()
470 "Return the Lisp list at point, or nil if none is found."
471 (form-at-point 'list 'listp))
1a2b6c52 472
55535639 473;;; thingatpt.el ends here