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