(vc-svn-admin-directory): New var.
[bpt/emacs.git] / lisp / thingatpt.el
CommitLineData
55535639 1;;; thingatpt.el --- get the `thing' at point
1a2b6c52 2
0d30b337 3;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000
aaef169d 4;; 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
1a2b6c52
RS
5
6;; Author: Mike Williams <mikew@gopher.dosli.govt.nz>
6254fc9f 7;; Maintainer: FSF
b7f66977 8;; Keywords: extensions, matching, mouse
1a2b6c52 9;; Created: Thu Mar 28 13:48:23 1991
1a2b6c52
RS
10
11;; This file is part of GNU Emacs.
12
13;; GNU Emacs is free software; you can redistribute it and/or modify
14;; it under the terms of the GNU General Public License as published by
15;; the Free Software Foundation; either version 2, or (at your option)
16;; any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
69f9ba7e 23;;; Commentary:
b578f267 24
c851323f
RS
25;; This file provides routines for getting the "thing" at the location of
26;; point, whatever that "thing" happens to be. The "thing" is defined by
7a8f27db 27;; its beginning and end positions in the buffer.
1a2b6c52
RS
28;;
29;; The function bounds-of-thing-at-point finds the beginning and end
c851323f 30;; positions by moving first forward to the end of the "thing", and then
1a2b6c52 31;; backwards to the beginning. By default, it uses the corresponding
c851323f 32;; forward-"thing" operator (eg. forward-word, forward-line).
1a2b6c52
RS
33;;
34;; Special cases are allowed for using properties associated with the named
f1180544 35;; "thing":
1a2b6c52 36;;
c851323f 37;; forward-op Function to call to skip forward over a "thing" (or
1a2b6c52 38;; with a negative argument, backward).
f1180544 39;;
c851323f
RS
40;; beginning-op Function to call to skip to the beginning of a "thing".
41;; end-op Function to call to skip to the end of a "thing".
1a2b6c52
RS
42;;
43;; Reliance on existing operators means that many `things' can be accessed
44;; without further code: eg.
45;; (thing-at-point 'line)
46;; (thing-at-point 'page)
47
b578f267 48;;; Code:
1a2b6c52
RS
49
50(provide 'thingatpt)
51
b578f267 52;; Basic movement
1a2b6c52
RS
53
54;;;###autoload
c851323f 55(defun forward-thing (thing &optional n)
2a59b30d 56 "Move forward to the end of the Nth next THING."
c851323f
RS
57 (let ((forward-op (or (get thing 'forward-op)
58 (intern-soft (format "forward-%s" thing)))))
6254fc9f 59 (if (functionp forward-op)
c851323f
RS
60 (funcall forward-op (or n 1))
61 (error "Can't determine how to move over a %s" thing))))
1a2b6c52 62
b578f267 63;; General routines
1a2b6c52
RS
64
65;;;###autoload
c851323f
RS
66(defun bounds-of-thing-at-point (thing)
67 "Determine the start and end buffer locations for the THING at point.
68THING is a symbol which specifies the kind of syntactic entity you want.
69Possibilities include `symbol', `list', `sexp', `defun', `filename', `url',
70`word', `sentence', `whitespace', `line', `page' and others.
71
72See the file `thingatpt.el' for documentation on how to define
73a symbol as a valid THING.
74
75The value is a cons cell (START . END) giving the start and end positions
76of the textual entity that was found."
d9cc804b
RS
77 (if (get thing 'bounds-of-thing-at-point)
78 (funcall (get thing 'bounds-of-thing-at-point))
79 (let ((orig (point)))
80 (condition-case nil
81 (save-excursion
82 ;; Try moving forward, then back.
2a59b30d
SM
83 (funcall ;; First move to end.
84 (or (get thing 'end-op)
85 (lambda () (forward-thing thing 1))))
86 (funcall ;; Then move to beg.
87 (or (get thing 'beginning-op)
88 (lambda () (forward-thing thing -1))))
89 (let ((beg (point)))
d9cc804b
RS
90 (if (not (and beg (> beg orig)))
91 ;; If that brings us all the way back to ORIG,
92 ;; it worked. But END may not be the real end.
93 ;; So find the real end that corresponds to BEG.
94 (let ((real-end
f1180544
JB
95 (progn
96 (funcall
97 (or (get thing 'end-op)
2a59b30d 98 (lambda () (forward-thing thing 1))))
d9cc804b
RS
99 (point))))
100 (if (and beg real-end (<= beg orig) (<= orig real-end))
101 (cons beg real-end)))
102 (goto-char orig)
103 ;; Try a second time, moving backward first and then forward,
104 ;; so that we can find a thing that ends at ORIG.
2a59b30d
SM
105 (funcall ;; First, move to beg.
106 (or (get thing 'beginning-op)
107 (lambda () (forward-thing thing -1))))
108 (funcall ;; Then move to end.
109 (or (get thing 'end-op)
110 (lambda () (forward-thing thing 1))))
111 (let ((end (point))
112 (real-beg
f1180544
JB
113 (progn
114 (funcall
115 (or (get thing 'beginning-op)
2a59b30d 116 (lambda () (forward-thing thing -1))))
d9cc804b
RS
117 (point))))
118 (if (and real-beg end (<= real-beg orig) (<= orig end))
119 (cons real-beg end))))))
120 (error nil)))))
1a2b6c52
RS
121
122;;;###autoload
c851323f
RS
123(defun thing-at-point (thing)
124 "Return the THING at point.
125THING is a symbol which specifies the kind of syntactic entity you want.
126Possibilities include `symbol', `list', `sexp', `defun', `filename', `url',
127`word', `sentence', `whitespace', `line', `page' and others.
128
129See the file `thingatpt.el' for documentation on how to define
130a symbol as a valid THING."
d9cc804b
RS
131 (if (get thing 'thing-at-point)
132 (funcall (get thing 'thing-at-point))
133 (let ((bounds (bounds-of-thing-at-point thing)))
f1180544 134 (if bounds
d9cc804b 135 (buffer-substring (car bounds) (cdr bounds))))))
1a2b6c52 136
b578f267 137;; Go to beginning/end
1a2b6c52 138
c851323f
RS
139(defun beginning-of-thing (thing)
140 (let ((bounds (bounds-of-thing-at-point thing)))
141 (or bounds (error "No %s here" thing))
1a2b6c52
RS
142 (goto-char (car bounds))))
143
c851323f
RS
144(defun end-of-thing (thing)
145 (let ((bounds (bounds-of-thing-at-point thing)))
146 (or bounds (error "No %s here" thing))
1a2b6c52
RS
147 (goto-char (cdr bounds))))
148
f1180544 149;; Special cases
1a2b6c52 150
f1180544 151;; Lines
9f5c7ace
RS
152
153;; bolp will be false when you click on the last line in the buffer
154;; and it has no final newline.
155
156(put 'line 'beginning-op
2a59b30d 157 (lambda () (if (bolp) (forward-line -1) (beginning-of-line))))
9f5c7ace 158
f1180544 159;; Sexps
1a2b6c52
RS
160
161(defun in-string-p ()
162 (let ((orig (point)))
163 (save-excursion
164 (beginning-of-defun)
165 (nth 3 (parse-partial-sexp (point) orig)))))
166
167(defun end-of-sexp ()
168 (let ((char-syntax (char-syntax (char-after (point)))))
169 (if (or (eq char-syntax ?\))
170 (and (eq char-syntax ?\") (in-string-p)))
171 (forward-char 1)
172 (forward-sexp 1))))
173
174(put 'sexp 'end-op 'end-of-sexp)
175
6f0e09d4
RS
176(defun beginning-of-sexp ()
177 (let ((char-syntax (char-syntax (char-before (point)))))
178 (if (or (eq char-syntax ?\()
179 (and (eq char-syntax ?\") (in-string-p)))
180 (forward-char -1)
181 (forward-sexp -1))))
182
183(put 'sexp 'beginning-op 'beginning-of-sexp)
184
f1180544 185;; Lists
1a2b6c52 186
2a59b30d 187(put 'list 'end-op (lambda () (up-list 1)))
1a2b6c52
RS
188(put 'list 'beginning-op 'backward-sexp)
189
4d61e7d5 190;; Filenames and URLs www.com/foo%32bar
1a2b6c52 191
839aacc9 192(defvar thing-at-point-file-name-chars "-~/[:alnum:]_.${}#%,:"
1a2b6c52
RS
193 "Characters allowable in filenames.")
194
f1180544 195(put 'filename 'end-op
839aacc9
DL
196 (lambda ()
197 (re-search-forward (concat "\\=[" thing-at-point-file-name-chars "]*")
198 nil t)))
1a2b6c52 199(put 'filename 'beginning-op
839aacc9
DL
200 (lambda ()
201 (if (re-search-backward (concat "[^" thing-at-point-file-name-chars "]")
202 nil t)
203 (forward-char)
204 (goto-char (point-min)))))
c851323f 205
d9cc804b
RS
206(defvar thing-at-point-url-path-regexp
207 "[^]\t\n \"'()<>[^`{}]*[^]\t\n \"'()<>[^`{}.,;]+"
0408f074 208 "A regular expression probably matching the host and filename or e-mail part of a URL.")
d9cc804b
RS
209
210(defvar thing-at-point-short-url-regexp
211 (concat "[-A-Za-z0-9.]+" thing-at-point-url-path-regexp)
212 "A regular expression probably matching a URL without an access scheme.
213Hostname matching is stricter in this case than for
214``thing-at-point-url-regexp''.")
215
1c1766c7
MR
216(defvar thing-at-point-uri-schemes
217 ;; Officials from http://www.iana.org/assignments/uri-schemes
218 '("ftp://" "http://" "gopher://" "mailto:" "news:" "nntp:"
219 "telnet://" "wais://" "file:/" "prospero:" "z39.50s:" "z39.50r:"
220 "cid:" "mid:" "vemmi:" "service:" "imap:" "nfs:" "acap:" "rtsp:"
221 "tip:" "pop:" "data:" "dav:" "opaquelocktoken:" "sip:" "tel:" "fax:"
222 "modem:" "ldap:" "https://" "soap.beep:" "soap.beeps:" "urn:" "go:"
223 "afs:" "tn3270:" "mailserver:"
224 ;; Compatibility
225 "snews:")
2a59b30d 226 "Uniform Resource Identifier (URI) Schemes.")
1c1766c7 227
d9cc804b 228(defvar thing-at-point-url-regexp
1c1766c7
MR
229 (concat "\\<\\(" (mapconcat 'identity thing-at-point-uri-schemes "\\|") "\\)"
230 thing-at-point-url-path-regexp)
d9cc804b
RS
231 "A regular expression probably matching a complete URL.")
232
233(defvar thing-at-point-markedup-url-regexp
234 "<URL:[^>]+>"
235 "A regular expression matching a URL marked up per RFC1738.
236This may contain whitespace (including newlines) .")
237
238(put 'url 'bounds-of-thing-at-point 'thing-at-point-bounds-of-url-at-point)
239(defun thing-at-point-bounds-of-url-at-point ()
2a59b30d
SM
240 (let ((strip (thing-at-point-looking-at
241 thing-at-point-markedup-url-regexp))) ;; (url "") short
242 (if (or strip
883d1f4b 243 (thing-at-point-looking-at thing-at-point-url-regexp)
d9cc804b 244 ;; Access scheme omitted?
2a59b30d
SM
245 ;; (setq short (thing-at-point-looking-at
246 ;; thing-at-point-short-url-regexp))
247 )
d9cc804b
RS
248 (let ((beginning (match-beginning 0))
249 (end (match-end 0)))
2a59b30d
SM
250 (when strip
251 (setq beginning (+ beginning 5))
252 (setq end (- end 1)))
d9cc804b
RS
253 (cons beginning end)))))
254
255(put 'url 'thing-at-point 'thing-at-point-url-at-point)
256(defun thing-at-point-url-at-point ()
257 "Return the URL around or before point.
340483df
DL
258
259Search backwards for the start of a URL ending at or after point. If
260no URL found, return nil. The access scheme will be prepended if
261absent: \"mailto:\" if the string contains \"@\", \"ftp://\" if it
262starts with \"ftp\" and not \"ftp:/\", or \"http://\" by default."
263
d9cc804b
RS
264 (let ((url "") short strip)
265 (if (or (setq strip (thing-at-point-looking-at
266 thing-at-point-markedup-url-regexp))
267 (thing-at-point-looking-at thing-at-point-url-regexp)
268 ;; Access scheme omitted?
269 (setq short (thing-at-point-looking-at
270 thing-at-point-short-url-regexp)))
271 (progn
272 (setq url (buffer-substring-no-properties (match-beginning 0)
273 (match-end 0)))
274 (and strip (setq url (substring url 5 -1))) ; Drop "<URL:" & ">"
275 ;; strip whitespace
84841dd1 276 (while (string-match "[ \t\n\r]+" url)
d9cc804b 277 (setq url (replace-match "" t t url)))
340483df
DL
278 (and short (setq url (concat (cond ((string-match "@" url)
279 "mailto:")
280 ;; e.g. ftp.swiss... or ftp-swiss...
281 ((string-match "^ftp" url)
282 "ftp://")
283 (t "http://"))
284 url)))
d9cc804b
RS
285 (if (string-equal "" url)
286 nil
287 url)))))
288
289;; The normal thingatpt mechanism doesn't work for complex regexps.
290;; This should work for almost any regexp wherever we are in the
291;; match. To do a perfect job for any arbitrary regexp would mean
292;; testing every position before point. Regexp searches won't find
293;; matches that straddle the start position so we search forwards once
294;; and then back repeatedly and then back up a char at a time.
295
296(defun thing-at-point-looking-at (regexp)
297 "Return non-nil if point is in or just after a match for REGEXP.
298Set the match data from the earliest such match ending at or after
299point."
300 (save-excursion
301 (let ((old-point (point)) match)
302 (and (looking-at regexp)
303 (>= (match-end 0) old-point)
304 (setq match (point)))
305 ;; Search back repeatedly from end of next match.
306 ;; This may fail if next match ends before this match does.
307 (re-search-forward regexp nil 'limit)
308 (while (and (re-search-backward regexp nil t)
309 (or (> (match-beginning 0) old-point)
310 (and (looking-at regexp) ; Extend match-end past search start
311 (>= (match-end 0) old-point)
312 (setq match (point))))))
313 (if (not match) nil
314 (goto-char match)
315 ;; Back up a char at a time in case search skipped
316 ;; intermediate match straddling search start pos.
317 (while (and (not (bobp))
318 (progn (backward-char 1) (looking-at regexp))
319 (>= (match-end 0) old-point)
320 (setq match (point))))
321 (goto-char match)
322 (looking-at regexp)))))
323
a1c9b4d0 324(put 'url 'end-op
2a59b30d
SM
325 (lambda ()
326 (let ((bounds (thing-at-point-bounds-of-url-at-point)))
327 (if bounds
328 (goto-char (cdr bounds))
329 (error "No URL here")))))
c851323f 330(put 'url 'beginning-op
2a59b30d
SM
331 (lambda ()
332 (let ((bounds (thing-at-point-bounds-of-url-at-point)))
333 (if bounds
334 (goto-char (car bounds))
335 (error "No URL here")))))
1a2b6c52 336
f1180544 337;; Whitespace
1a2b6c52 338
c851323f 339(defun forward-whitespace (arg)
1a2b6c52 340 (interactive "p")
f1180544 341 (if (natnump arg)
9e594a2e 342 (re-search-forward "[ \t]+\\|\n" nil 'move arg)
c851323f 343 (while (< arg 0)
9e594a2e 344 (if (re-search-backward "[ \t]+\\|\n" nil 'move)
1a2b6c52
RS
345 (or (eq (char-after (match-beginning 0)) 10)
346 (skip-chars-backward " \t")))
c851323f 347 (setq arg (1+ arg)))))
1a2b6c52 348
f1180544 349;; Buffer
1a2b6c52 350
206eef6c
SM
351(put 'buffer 'end-op (lambda () (goto-char (point-max))))
352(put 'buffer 'beginning-op (lambda () (goto-char (point-min))))
1a2b6c52 353
f1180544 354;; Symbols
1a2b6c52 355
c851323f 356(defun forward-symbol (arg)
1a2b6c52 357 (interactive "p")
f1180544 358 (if (natnump arg)
9e594a2e 359 (re-search-forward "\\(\\sw\\|\\s_\\)+" nil 'move arg)
c851323f 360 (while (< arg 0)
9e594a2e 361 (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil 'move)
1a2b6c52 362 (skip-syntax-backward "w_"))
c851323f 363 (setq arg (1+ arg)))))
1a2b6c52 364
f1180544 365;; Syntax blocks
fde7326e
RS
366
367(defun forward-same-syntax (&optional arg)
368 (interactive "p")
369 (while (< arg 0)
f1180544 370 (skip-syntax-backward
fde7326e
RS
371 (char-to-string (char-syntax (char-after (1- (point))))))
372 (setq arg (1+ arg)))
373 (while (> arg 0)
374 (skip-syntax-forward (char-to-string (char-syntax (char-after (point)))))
375 (setq arg (1- arg))))
376
f1180544 377;; Aliases
1a2b6c52
RS
378
379(defun word-at-point () (thing-at-point 'word))
380(defun sentence-at-point () (thing-at-point 'sentence))
381
c851323f 382(defun read-from-whole-string (str)
2a59b30d 383 "Read a Lisp expression from STR.
c851323f
RS
384Signal an error if the entire string was not used."
385 (let* ((read-data (read-from-string str))
f1180544 386 (more-left
1a2b6c52 387 (condition-case nil
1b0d40de 388 ;; The call to `ignore' suppresses a compiler warning.
7f1422bc 389 (progn (ignore (read-from-string (substring str (cdr read-data))))
1a2b6c52
RS
390 t)
391 (end-of-file nil))))
392 (if more-left
393 (error "Can't read whole string")
394 (car read-data))))
395
f1180544
JB
396(defun form-at-point (&optional thing pred)
397 (let ((sexp (condition-case nil
c851323f 398 (read-from-whole-string (thing-at-point (or thing 'sexp)))
1a2b6c52 399 (error nil))))
c851323f 400 (if (or (not pred) (funcall pred sexp)) sexp)))
1a2b6c52 401
be64abcf 402;;;###autoload
1a2b6c52 403(defun sexp-at-point () (form-at-point 'sexp))
be64abcf 404;;;###autoload
2a59b30d
SM
405(defun symbol-at-point ()
406 (let ((thing (thing-at-point 'symbol)))
407 (if thing (intern thing))))
be64abcf 408;;;###autoload
1a2b6c52 409(defun number-at-point () (form-at-point 'sexp 'numberp))
be64abcf 410;;;###autoload
1a2b6c52
RS
411(defun list-at-point () (form-at-point 'list 'listp))
412
2a59b30d 413;; arch-tag: bb65a163-dae2-4055-aedc-fe11f497f698
55535639 414;;; thingatpt.el ends here