]> code.delx.au - gnu-emacs/blob - lisp/thingatpt.el
(sit_for): New arg initial_display.
[gnu-emacs] / lisp / thingatpt.el
1 ;;; thingatpt.el --- Get the `thing' at point
2
3 ;; Copyright (C) 1991,92,93,94,95,1996 Free Software Foundation, Inc.
4
5 ;; Author: Mike Williams <mikew@gopher.dosli.govt.nz>
6 ;; Keywords: extensions, matching, mouse
7 ;; Created: Thu Mar 28 13:48:23 1991
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
21 ;;; Commentary:
22
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
25 ;; its beginning and end positions in the buffer.
26 ;;
27 ;; The function bounds-of-thing-at-point finds the beginning and end
28 ;; positions by moving first forward to the end of the "thing", and then
29 ;; backwards to the beginning. By default, it uses the corresponding
30 ;; forward-"thing" operator (eg. forward-word, forward-line).
31 ;;
32 ;; Special cases are allowed for using properties associated with the named
33 ;; "thing":
34 ;;
35 ;; forward-op Function to call to skip forward over a "thing" (or
36 ;; with a negative argument, backward).
37 ;;
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".
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
46 ;;; Code:
47
48 (provide 'thingatpt)
49
50 ;; Basic movement
51
52 ;;;###autoload
53 (defun forward-thing (thing &optional n)
54 "Move forward to the end of the next THING."
55 (let ((forward-op (or (get thing 'forward-op)
56 (intern-soft (format "forward-%s" thing)))))
57 (if (fboundp forward-op)
58 (funcall forward-op (or n 1))
59 (error "Can't determine how to move over a %s" thing))))
60
61 ;; General routines
62
63 ;;;###autoload
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."
75 (let ((orig (point)))
76 (condition-case nil
77 (save-excursion
78 ;; Try moving forward, then back.
79 (let ((end (progn
80 (funcall
81 (or (get thing 'end-op)
82 (function (lambda () (forward-thing thing 1)))))
83 (point)))
84 (beg (progn
85 (funcall
86 (or (get thing 'beginning-op)
87 (function (lambda () (forward-thing thing -1)))))
88 (point))))
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,
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)))))
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))))))
122 (error nil))))
123
124 ;;;###autoload
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)))
134 (if bounds
135 (buffer-substring (car bounds) (cdr bounds)))))
136
137 ;; Go to beginning/end
138
139 (defun beginning-of-thing (thing)
140 (let ((bounds (bounds-of-thing-at-point thing)))
141 (or bounds (error "No %s here" thing))
142 (goto-char (car bounds))))
143
144 (defun end-of-thing (thing)
145 (let ((bounds (bounds-of-thing-at-point thing)))
146 (or bounds (error "No %s here" thing))
147 (goto-char (cdr bounds))))
148
149 ;; Special cases
150
151 ;; Lines
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
159 ;; Sexps
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
176 ;; Lists
177
178 (put 'list 'end-op (function (lambda () (up-list 1))))
179 (put 'list 'beginning-op 'backward-sexp)
180
181 ;; Filenames and URLs
182
183 (defvar thing-at-point-file-name-chars "~/A-Za-z0-9---_.${}#%,:"
184 "Characters allowable in filenames.")
185
186 (put 'filename 'end-op
187 '(lambda () (skip-chars-forward thing-at-point-file-name-chars)))
188 (put 'filename 'beginning-op
189 '(lambda () (skip-chars-backward thing-at-point-file-name-chars)))
190
191 (defvar thing-at-point-url-chars "~/A-Za-z0-9---_@$%&=.,"
192 "Characters allowable in a URL.")
193
194 (put 'url 'end-op
195 '(lambda () (skip-chars-forward (concat ":" thing-at-point-url-chars))
196 (skip-chars-backward ".,:")))
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")))
204
205 ;; Whitespace
206
207 (defun forward-whitespace (arg)
208 (interactive "p")
209 (if (natnump arg)
210 (re-search-forward "[ \t]+\\|\n" nil 'move arg)
211 (while (< arg 0)
212 (if (re-search-backward "[ \t]+\\|\n" nil 'move)
213 (or (eq (char-after (match-beginning 0)) 10)
214 (skip-chars-backward " \t")))
215 (setq arg (1+ arg)))))
216
217 ;; Buffer
218
219 (put 'buffer 'end-op 'end-of-buffer)
220 (put 'buffer 'beginning-op 'beginning-of-buffer)
221
222 ;; Symbols
223
224 (defun forward-symbol (arg)
225 (interactive "p")
226 (if (natnump arg)
227 (re-search-forward "\\(\\sw\\|\\s_\\)+" nil 'move arg)
228 (while (< arg 0)
229 (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil 'move)
230 (skip-syntax-backward "w_"))
231 (setq arg (1+ arg)))))
232
233 ;; Syntax blocks
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
245 ;; Aliases
246
247 (defun word-at-point () (thing-at-point 'word))
248 (defun sentence-at-point () (thing-at-point 'sentence))
249
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))
254 (more-left
255 (condition-case nil
256 (progn (read-from-string (substring str (cdr read-data)))
257 t)
258 (end-of-file nil))))
259 (if more-left
260 (error "Can't read whole string")
261 (car read-data))))
262
263 (defun form-at-point (&optional thing pred)
264 (let ((sexp (condition-case nil
265 (read-from-whole-string (thing-at-point (or thing 'sexp)))
266 (error nil))))
267 (if (or (not pred) (funcall pred sexp)) sexp)))
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.