Commit | Line | Data |
---|---|---|
1a2b6c52 RS |
1 | ;;; thingatpt.el --- Get the `thing' at point |
2 | ||
c851323f | 3 | ;; Copyright (C) 1991,92,93,94,95,1996 Free Software Foundation, Inc. |
1a2b6c52 RS |
4 | |
5 | ;; Author: Mike Williams <mikew@gopher.dosli.govt.nz> | |
b7f66977 | 6 | ;; Keywords: extensions, matching, mouse |
1a2b6c52 | 7 | ;; Created: Thu Mar 28 13:48:23 1991 |
1a2b6c52 RS |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
69f9ba7e | 21 | ;;; Commentary: |
b578f267 | 22 | |
c851323f RS |
23 | ;; This file provides routines for getting the "thing" at the location of |
24 | ;; point, whatever that "thing" happens to be. The "thing" is defined by | |
7a8f27db | 25 | ;; its beginning and end positions in the buffer. |
1a2b6c52 RS |
26 | ;; |
27 | ;; The function bounds-of-thing-at-point finds the beginning and end | |
c851323f | 28 | ;; positions by moving first forward to the end of the "thing", and then |
1a2b6c52 | 29 | ;; backwards to the beginning. By default, it uses the corresponding |
c851323f | 30 | ;; forward-"thing" operator (eg. forward-word, forward-line). |
1a2b6c52 RS |
31 | ;; |
32 | ;; Special cases are allowed for using properties associated with the named | |
c851323f | 33 | ;; "thing": |
1a2b6c52 | 34 | ;; |
c851323f | 35 | ;; forward-op Function to call to skip forward over a "thing" (or |
1a2b6c52 RS |
36 | ;; with a negative argument, backward). |
37 | ;; | |
c851323f RS |
38 | ;; beginning-op Function to call to skip to the beginning of a "thing". |
39 | ;; end-op Function to call to skip to the end of a "thing". | |
1a2b6c52 RS |
40 | ;; |
41 | ;; Reliance on existing operators means that many `things' can be accessed | |
42 | ;; without further code: eg. | |
43 | ;; (thing-at-point 'line) | |
44 | ;; (thing-at-point 'page) | |
45 | ||
b578f267 | 46 | ;;; Code: |
1a2b6c52 RS |
47 | |
48 | (provide 'thingatpt) | |
49 | ||
b578f267 | 50 | ;; Basic movement |
1a2b6c52 RS |
51 | |
52 | ;;;###autoload | |
c851323f | 53 | (defun forward-thing (thing &optional n) |
1a2b6c52 | 54 | "Move forward to the end of the next THING." |
c851323f RS |
55 | (let ((forward-op (or (get thing 'forward-op) |
56 | (intern-soft (format "forward-%s" thing))))) | |
1a2b6c52 | 57 | (if (fboundp forward-op) |
c851323f RS |
58 | (funcall forward-op (or n 1)) |
59 | (error "Can't determine how to move over a %s" thing)))) | |
1a2b6c52 | 60 | |
b578f267 | 61 | ;; General routines |
1a2b6c52 RS |
62 | |
63 | ;;;###autoload | |
c851323f RS |
64 | (defun bounds-of-thing-at-point (thing) |
65 | "Determine the start and end buffer locations for the THING at point. | |
66 | THING is a symbol which specifies the kind of syntactic entity you want. | |
67 | Possibilities include `symbol', `list', `sexp', `defun', `filename', `url', | |
68 | `word', `sentence', `whitespace', `line', `page' and others. | |
69 | ||
70 | See the file `thingatpt.el' for documentation on how to define | |
71 | a symbol as a valid THING. | |
72 | ||
73 | The value is a cons cell (START . END) giving the start and end positions | |
74 | of the textual entity that was found." | |
1a2b6c52 RS |
75 | (let ((orig (point))) |
76 | (condition-case nil | |
77 | (save-excursion | |
9e594a2e | 78 | ;; Try moving forward, then back. |
1a2b6c52 RS |
79 | (let ((end (progn |
80 | (funcall | |
c851323f RS |
81 | (or (get thing 'end-op) |
82 | (function (lambda () (forward-thing thing 1))))) | |
1a2b6c52 RS |
83 | (point))) |
84 | (beg (progn | |
85 | (funcall | |
c851323f RS |
86 | (or (get thing 'beginning-op) |
87 | (function (lambda () (forward-thing thing -1))))) | |
1a2b6c52 | 88 | (point)))) |
9e594a2e RS |
89 | (if (not (and beg (> beg orig))) |
90 | ;; If that brings us all the way back to ORIG, | |
91 | ;; it worked. But END may not be the real end. | |
92 | ;; So find the real end that corresponds to BEG. | |
93 | (let ((real-end | |
94 | (progn | |
95 | (funcall | |
96 | (or (get thing 'end-op) | |
97 | (function (lambda () (forward-thing thing 1))))) | |
98 | (point)))) | |
99 | (if (and beg real-end (<= beg orig) (<= orig real-end)) | |
100 | (cons beg real-end))) | |
101 | (goto-char orig) | |
102 | ;; Try a second time, moving backward first and then forward, | |
02807c95 RS |
103 | ;; so that we can find a thing that ends at ORIG. |
104 | (let ((beg (progn | |
105 | (funcall | |
106 | (or (get thing 'beginning-op) | |
107 | (function (lambda () (forward-thing thing -1))))) | |
108 | (point))) | |
109 | (end (progn | |
110 | (funcall | |
111 | (or (get thing 'end-op) | |
112 | (function (lambda () (forward-thing thing 1))))) | |
9e594a2e RS |
113 | (point))) |
114 | (real-beg | |
115 | (progn | |
116 | (funcall | |
117 | (or (get thing 'end-op) | |
118 | (function (lambda () (forward-thing thing -1))))) | |
119 | (point)))) | |
120 | (if (and real-beg end (<= real-beg orig) (<= orig end)) | |
121 | (cons real-beg end)))))) | |
1a2b6c52 RS |
122 | (error nil)))) |
123 | ||
124 | ;;;###autoload | |
c851323f RS |
125 | (defun thing-at-point (thing) |
126 | "Return the THING at point. | |
127 | THING is a symbol which specifies the kind of syntactic entity you want. | |
128 | Possibilities include `symbol', `list', `sexp', `defun', `filename', `url', | |
129 | `word', `sentence', `whitespace', `line', `page' and others. | |
130 | ||
131 | See the file `thingatpt.el' for documentation on how to define | |
132 | a symbol as a valid THING." | |
133 | (let ((bounds (bounds-of-thing-at-point thing))) | |
1a2b6c52 RS |
134 | (if bounds |
135 | (buffer-substring (car bounds) (cdr bounds))))) | |
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 | ||
b578f267 | 149 | ;; Special cases |
1a2b6c52 | 150 | |
b578f267 | 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 | |
157 | (function (lambda () (if (bolp) (forward-line -1) (beginning-of-line))))) | |
158 | ||
b578f267 | 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 | ||
b578f267 | 176 | ;; Lists |
1a2b6c52 RS |
177 | |
178 | (put 'list 'end-op (function (lambda () (up-list 1)))) | |
179 | (put 'list 'beginning-op 'backward-sexp) | |
180 | ||
c851323f | 181 | ;; Filenames and URLs |
1a2b6c52 | 182 | |
c851323f | 183 | (defvar thing-at-point-file-name-chars "~/A-Za-z0-9---_.${}#%,:" |
1a2b6c52 RS |
184 | "Characters allowable in filenames.") |
185 | ||
186 | (put 'filename 'end-op | |
c851323f | 187 | '(lambda () (skip-chars-forward thing-at-point-file-name-chars))) |
1a2b6c52 | 188 | (put 'filename 'beginning-op |
c851323f RS |
189 | '(lambda () (skip-chars-backward thing-at-point-file-name-chars))) |
190 | ||
02807c95 | 191 | (defvar thing-at-point-url-chars "~/A-Za-z0-9---_@$%&=.," |
c851323f RS |
192 | "Characters allowable in a URL.") |
193 | ||
194 | (put 'url 'end-op | |
02807c95 RS |
195 | '(lambda () (skip-chars-forward (concat ":" thing-at-point-url-chars)) |
196 | (skip-chars-backward ".,:"))) | |
c851323f RS |
197 | (put 'url 'beginning-op |
198 | '(lambda () | |
199 | (skip-chars-backward thing-at-point-url-chars) | |
200 | (or (= (preceding-char) ?:) | |
201 | (error "No URL here")) | |
202 | (forward-char -1) | |
203 | (skip-chars-backward "a-zA-Z"))) | |
1a2b6c52 | 204 | |
b578f267 | 205 | ;; Whitespace |
1a2b6c52 | 206 | |
c851323f | 207 | (defun forward-whitespace (arg) |
1a2b6c52 | 208 | (interactive "p") |
c851323f | 209 | (if (natnump arg) |
9e594a2e | 210 | (re-search-forward "[ \t]+\\|\n" nil 'move arg) |
c851323f | 211 | (while (< arg 0) |
9e594a2e | 212 | (if (re-search-backward "[ \t]+\\|\n" nil 'move) |
1a2b6c52 RS |
213 | (or (eq (char-after (match-beginning 0)) 10) |
214 | (skip-chars-backward " \t"))) | |
c851323f | 215 | (setq arg (1+ arg))))) |
1a2b6c52 | 216 | |
b578f267 | 217 | ;; Buffer |
1a2b6c52 RS |
218 | |
219 | (put 'buffer 'end-op 'end-of-buffer) | |
220 | (put 'buffer 'beginning-op 'beginning-of-buffer) | |
221 | ||
b578f267 | 222 | ;; Symbols |
1a2b6c52 | 223 | |
c851323f | 224 | (defun forward-symbol (arg) |
1a2b6c52 | 225 | (interactive "p") |
c851323f | 226 | (if (natnump arg) |
9e594a2e | 227 | (re-search-forward "\\(\\sw\\|\\s_\\)+" nil 'move arg) |
c851323f | 228 | (while (< arg 0) |
9e594a2e | 229 | (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil 'move) |
1a2b6c52 | 230 | (skip-syntax-backward "w_")) |
c851323f | 231 | (setq arg (1+ arg))))) |
1a2b6c52 | 232 | |
b578f267 | 233 | ;; Syntax blocks |
fde7326e RS |
234 | |
235 | (defun forward-same-syntax (&optional arg) | |
236 | (interactive "p") | |
237 | (while (< arg 0) | |
238 | (skip-syntax-backward | |
239 | (char-to-string (char-syntax (char-after (1- (point)))))) | |
240 | (setq arg (1+ arg))) | |
241 | (while (> arg 0) | |
242 | (skip-syntax-forward (char-to-string (char-syntax (char-after (point))))) | |
243 | (setq arg (1- arg)))) | |
244 | ||
b578f267 | 245 | ;; Aliases |
1a2b6c52 RS |
246 | |
247 | (defun word-at-point () (thing-at-point 'word)) | |
248 | (defun sentence-at-point () (thing-at-point 'sentence)) | |
249 | ||
c851323f RS |
250 | (defun read-from-whole-string (str) |
251 | "Read a lisp expression from STR. | |
252 | Signal an error if the entire string was not used." | |
253 | (let* ((read-data (read-from-string str)) | |
1a2b6c52 RS |
254 | (more-left |
255 | (condition-case nil | |
c851323f | 256 | (progn (read-from-string (substring str (cdr read-data))) |
1a2b6c52 RS |
257 | t) |
258 | (end-of-file nil)))) | |
259 | (if more-left | |
260 | (error "Can't read whole string") | |
261 | (car read-data)))) | |
262 | ||
c851323f | 263 | (defun form-at-point (&optional thing pred) |
1a2b6c52 | 264 | (let ((sexp (condition-case nil |
c851323f | 265 | (read-from-whole-string (thing-at-point (or thing 'sexp))) |
1a2b6c52 | 266 | (error nil)))) |
c851323f | 267 | (if (or (not pred) (funcall pred sexp)) sexp))) |
1a2b6c52 RS |
268 | |
269 | (defun sexp-at-point () (form-at-point 'sexp)) | |
270 | (defun symbol-at-point () (form-at-point 'sexp 'symbolp)) | |
271 | (defun number-at-point () (form-at-point 'sexp 'numberp)) | |
272 | (defun list-at-point () (form-at-point 'list 'listp)) | |
273 | ||
274 | ;; thingatpt.el ends here. |