Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; thingatpt.el --- get the `thing' at point |
1a2b6c52 | 2 | |
6254fc9f GM |
3 | ;; Copyright (C) 1991,92,93,94,95,96,97,1998,2000 |
4 | ;; 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 | |
c851323f | 35 | ;; "thing": |
1a2b6c52 | 36 | ;; |
c851323f | 37 | ;; forward-op Function to call to skip forward over a "thing" (or |
1a2b6c52 RS |
38 | ;; with a negative argument, backward). |
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) |
1a2b6c52 | 56 | "Move forward to the end of the 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. | |
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." | |
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. | |
83 | (let ((end (progn | |
9e594a2e RS |
84 | (funcall |
85 | (or (get thing 'end-op) | |
86 | (function (lambda () (forward-thing thing 1))))) | |
d9cc804b RS |
87 | (point))) |
88 | (beg (progn | |
89 | (funcall | |
90 | (or (get thing 'beginning-op) | |
91 | (function (lambda () (forward-thing thing -1))))) | |
9e594a2e | 92 | (point)))) |
d9cc804b RS |
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 | |
02807c95 RS |
99 | (funcall |
100 | (or (get thing 'end-op) | |
101 | (function (lambda () (forward-thing thing 1))))) | |
d9cc804b RS |
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))))) | |
1a2b6c52 RS |
127 | |
128 | ;;;###autoload | |
c851323f RS |
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." | |
d9cc804b RS |
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)))))) | |
1a2b6c52 | 142 | |
b578f267 | 143 | ;; Go to beginning/end |
1a2b6c52 | 144 | |
c851323f RS |
145 | (defun beginning-of-thing (thing) |
146 | (let ((bounds (bounds-of-thing-at-point thing))) | |
147 | (or bounds (error "No %s here" thing)) | |
1a2b6c52 RS |
148 | (goto-char (car bounds)))) |
149 | ||
c851323f RS |
150 | (defun end-of-thing (thing) |
151 | (let ((bounds (bounds-of-thing-at-point thing))) | |
152 | (or bounds (error "No %s here" thing)) | |
1a2b6c52 RS |
153 | (goto-char (cdr bounds)))) |
154 | ||
b578f267 | 155 | ;; Special cases |
1a2b6c52 | 156 | |
b578f267 | 157 | ;; Lines |
9f5c7ace RS |
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 | ||
b578f267 | 165 | ;; Sexps |
1a2b6c52 RS |
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 | ||
6f0e09d4 RS |
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 | ||
b578f267 | 191 | ;; Lists |
1a2b6c52 RS |
192 | |
193 | (put 'list 'end-op (function (lambda () (up-list 1)))) | |
194 | (put 'list 'beginning-op 'backward-sexp) | |
195 | ||
c851323f | 196 | ;; Filenames and URLs |
1a2b6c52 | 197 | |
c851323f | 198 | (defvar thing-at-point-file-name-chars "~/A-Za-z0-9---_.${}#%,:" |
1a2b6c52 RS |
199 | "Characters allowable in filenames.") |
200 | ||
201 | (put 'filename 'end-op | |
206eef6c | 202 | (lambda () (skip-chars-forward thing-at-point-file-name-chars))) |
1a2b6c52 | 203 | (put 'filename 'beginning-op |
206eef6c | 204 | (lambda () (skip-chars-backward thing-at-point-file-name-chars))) |
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. | |
213 | Hostname 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:") | |
226 | "Uniform Resource Identifier (URI) Schemes") | |
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. | |
236 | This 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 () | |
240 | (let ((url "") short strip) | |
241 | (if (or (setq strip (thing-at-point-looking-at | |
242 | thing-at-point-markedup-url-regexp)) | |
243 | (thing-at-point-looking-at thing-at-point-url-regexp) | |
244 | ;; Access scheme omitted? | |
245 | (setq short (thing-at-point-looking-at | |
246 | thing-at-point-short-url-regexp))) | |
247 | (let ((beginning (match-beginning 0)) | |
248 | (end (match-end 0))) | |
249 | (cond (strip | |
250 | (setq beginning (+ beginning 5)) | |
251 | (setq end (- end 1)))) | |
252 | (cons beginning end))))) | |
253 | ||
254 | (put 'url 'thing-at-point 'thing-at-point-url-at-point) | |
255 | (defun thing-at-point-url-at-point () | |
256 | "Return the URL around or before point. | |
340483df DL |
257 | |
258 | Search backwards for the start of a URL ending at or after point. If | |
259 | no URL found, return nil. The access scheme will be prepended if | |
260 | absent: \"mailto:\" if the string contains \"@\", \"ftp://\" if it | |
261 | starts with \"ftp\" and not \"ftp:/\", or \"http://\" by default." | |
262 | ||
d9cc804b RS |
263 | (let ((url "") short strip) |
264 | (if (or (setq strip (thing-at-point-looking-at | |
265 | thing-at-point-markedup-url-regexp)) | |
266 | (thing-at-point-looking-at thing-at-point-url-regexp) | |
267 | ;; Access scheme omitted? | |
268 | (setq short (thing-at-point-looking-at | |
269 | thing-at-point-short-url-regexp))) | |
270 | (progn | |
271 | (setq url (buffer-substring-no-properties (match-beginning 0) | |
272 | (match-end 0))) | |
273 | (and strip (setq url (substring url 5 -1))) ; Drop "<URL:" & ">" | |
274 | ;; strip whitespace | |
84841dd1 | 275 | (while (string-match "[ \t\n\r]+" url) |
d9cc804b | 276 | (setq url (replace-match "" t t url))) |
340483df DL |
277 | (and short (setq url (concat (cond ((string-match "@" url) |
278 | "mailto:") | |
279 | ;; e.g. ftp.swiss... or ftp-swiss... | |
280 | ((string-match "^ftp" url) | |
281 | "ftp://") | |
282 | (t "http://")) | |
283 | url))) | |
d9cc804b RS |
284 | (if (string-equal "" url) |
285 | nil | |
286 | url))))) | |
287 | ||
288 | ;; The normal thingatpt mechanism doesn't work for complex regexps. | |
289 | ;; This should work for almost any regexp wherever we are in the | |
290 | ;; match. To do a perfect job for any arbitrary regexp would mean | |
291 | ;; testing every position before point. Regexp searches won't find | |
292 | ;; matches that straddle the start position so we search forwards once | |
293 | ;; and then back repeatedly and then back up a char at a time. | |
294 | ||
295 | (defun thing-at-point-looking-at (regexp) | |
296 | "Return non-nil if point is in or just after a match for REGEXP. | |
297 | Set the match data from the earliest such match ending at or after | |
298 | point." | |
299 | (save-excursion | |
300 | (let ((old-point (point)) match) | |
301 | (and (looking-at regexp) | |
302 | (>= (match-end 0) old-point) | |
303 | (setq match (point))) | |
304 | ;; Search back repeatedly from end of next match. | |
305 | ;; This may fail if next match ends before this match does. | |
306 | (re-search-forward regexp nil 'limit) | |
307 | (while (and (re-search-backward regexp nil t) | |
308 | (or (> (match-beginning 0) old-point) | |
309 | (and (looking-at regexp) ; Extend match-end past search start | |
310 | (>= (match-end 0) old-point) | |
311 | (setq match (point)))))) | |
312 | (if (not match) nil | |
313 | (goto-char match) | |
314 | ;; Back up a char at a time in case search skipped | |
315 | ;; intermediate match straddling search start pos. | |
316 | (while (and (not (bobp)) | |
317 | (progn (backward-char 1) (looking-at regexp)) | |
318 | (>= (match-end 0) old-point) | |
319 | (setq match (point)))) | |
320 | (goto-char match) | |
321 | (looking-at regexp))))) | |
322 | ||
a1c9b4d0 RS |
323 | (put 'url 'end-op |
324 | (function (lambda () | |
325 | (let ((bounds (thing-at-point-bounds-of-url-at-point))) | |
326 | (if bounds | |
327 | (goto-char (cdr bounds)) | |
328 | (error "No URL here")))))) | |
c851323f | 329 | (put 'url 'beginning-op |
a1c9b4d0 RS |
330 | (function (lambda () |
331 | (let ((bounds (thing-at-point-bounds-of-url-at-point))) | |
332 | (if bounds | |
333 | (goto-char (car bounds)) | |
334 | (error "No URL here")))))) | |
1a2b6c52 | 335 | |
b578f267 | 336 | ;; Whitespace |
1a2b6c52 | 337 | |
c851323f | 338 | (defun forward-whitespace (arg) |
1a2b6c52 | 339 | (interactive "p") |
c851323f | 340 | (if (natnump arg) |
9e594a2e | 341 | (re-search-forward "[ \t]+\\|\n" nil 'move arg) |
c851323f | 342 | (while (< arg 0) |
9e594a2e | 343 | (if (re-search-backward "[ \t]+\\|\n" nil 'move) |
1a2b6c52 RS |
344 | (or (eq (char-after (match-beginning 0)) 10) |
345 | (skip-chars-backward " \t"))) | |
c851323f | 346 | (setq arg (1+ arg))))) |
1a2b6c52 | 347 | |
b578f267 | 348 | ;; Buffer |
1a2b6c52 | 349 | |
206eef6c SM |
350 | (put 'buffer 'end-op (lambda () (goto-char (point-max)))) |
351 | (put 'buffer 'beginning-op (lambda () (goto-char (point-min)))) | |
1a2b6c52 | 352 | |
b578f267 | 353 | ;; Symbols |
1a2b6c52 | 354 | |
c851323f | 355 | (defun forward-symbol (arg) |
1a2b6c52 | 356 | (interactive "p") |
c851323f | 357 | (if (natnump arg) |
9e594a2e | 358 | (re-search-forward "\\(\\sw\\|\\s_\\)+" nil 'move arg) |
c851323f | 359 | (while (< arg 0) |
9e594a2e | 360 | (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil 'move) |
1a2b6c52 | 361 | (skip-syntax-backward "w_")) |
c851323f | 362 | (setq arg (1+ arg))))) |
1a2b6c52 | 363 | |
b578f267 | 364 | ;; Syntax blocks |
fde7326e RS |
365 | |
366 | (defun forward-same-syntax (&optional arg) | |
367 | (interactive "p") | |
368 | (while (< arg 0) | |
369 | (skip-syntax-backward | |
370 | (char-to-string (char-syntax (char-after (1- (point)))))) | |
371 | (setq arg (1+ arg))) | |
372 | (while (> arg 0) | |
373 | (skip-syntax-forward (char-to-string (char-syntax (char-after (point))))) | |
374 | (setq arg (1- arg)))) | |
375 | ||
b578f267 | 376 | ;; Aliases |
1a2b6c52 RS |
377 | |
378 | (defun word-at-point () (thing-at-point 'word)) | |
379 | (defun sentence-at-point () (thing-at-point 'sentence)) | |
380 | ||
c851323f RS |
381 | (defun read-from-whole-string (str) |
382 | "Read a lisp expression from STR. | |
383 | Signal an error if the entire string was not used." | |
384 | (let* ((read-data (read-from-string str)) | |
1a2b6c52 RS |
385 | (more-left |
386 | (condition-case nil | |
1b0d40de | 387 | ;; The call to `ignore' suppresses a compiler warning. |
7f1422bc | 388 | (progn (ignore (read-from-string (substring str (cdr read-data)))) |
1a2b6c52 RS |
389 | t) |
390 | (end-of-file nil)))) | |
391 | (if more-left | |
392 | (error "Can't read whole string") | |
393 | (car read-data)))) | |
394 | ||
c851323f | 395 | (defun form-at-point (&optional thing pred) |
1a2b6c52 | 396 | (let ((sexp (condition-case nil |
c851323f | 397 | (read-from-whole-string (thing-at-point (or thing 'sexp))) |
1a2b6c52 | 398 | (error nil)))) |
c851323f | 399 | (if (or (not pred) (funcall pred sexp)) sexp))) |
1a2b6c52 | 400 | |
be64abcf | 401 | ;;;###autoload |
1a2b6c52 | 402 | (defun sexp-at-point () (form-at-point 'sexp)) |
be64abcf | 403 | ;;;###autoload |
1a2b6c52 | 404 | (defun symbol-at-point () (form-at-point 'sexp 'symbolp)) |
be64abcf | 405 | ;;;###autoload |
1a2b6c52 | 406 | (defun number-at-point () (form-at-point 'sexp 'numberp)) |
be64abcf | 407 | ;;;###autoload |
1a2b6c52 RS |
408 | (defun list-at-point () (form-at-point 'list 'listp)) |
409 | ||
55535639 | 410 | ;;; thingatpt.el ends here |