*** empty log message ***
[bpt/emacs.git] / lisp / thingatpt.el
CommitLineData
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.
66THING is a symbol which specifies the kind of syntactic entity you want.
67Possibilities include `symbol', `list', `sexp', `defun', `filename', `url',
68`word', `sentence', `whitespace', `line', `page' and others.
69
70See the file `thingatpt.el' for documentation on how to define
71a symbol as a valid THING.
72
73The value is a cons cell (START . END) giving the start and end positions
74of the textual entity that was found."
1a2b6c52
RS
75 (let ((orig (point)))
76 (condition-case nil
77 (save-excursion
78 (let ((end (progn
79 (funcall
c851323f
RS
80 (or (get thing 'end-op)
81 (function (lambda () (forward-thing thing 1)))))
1a2b6c52
RS
82 (point)))
83 (beg (progn
84 (funcall
c851323f
RS
85 (or (get thing 'beginning-op)
86 (function (lambda () (forward-thing thing -1)))))
1a2b6c52 87 (point))))
02807c95
RS
88 (if (and beg end (<= beg orig) (<= orig end))
89 (cons beg end)
90 ;; Try a second time, moving backward first and forward after,
91 ;; so that we can find a thing that ends at ORIG.
92 (let ((beg (progn
93 (funcall
94 (or (get thing 'beginning-op)
95 (function (lambda () (forward-thing thing -1)))))
96 (point)))
97 (end (progn
98 (funcall
99 (or (get thing 'end-op)
100 (function (lambda () (forward-thing thing 1)))))
101 (point))))
102 (if (and beg end (<= beg orig) (<= orig end))
103 (cons beg end))))))
1a2b6c52
RS
104 (error nil))))
105
106;;;###autoload
c851323f
RS
107(defun thing-at-point (thing)
108 "Return the THING at point.
109THING is a symbol which specifies the kind of syntactic entity you want.
110Possibilities include `symbol', `list', `sexp', `defun', `filename', `url',
111`word', `sentence', `whitespace', `line', `page' and others.
112
113See the file `thingatpt.el' for documentation on how to define
114a symbol as a valid THING."
115 (let ((bounds (bounds-of-thing-at-point thing)))
1a2b6c52
RS
116 (if bounds
117 (buffer-substring (car bounds) (cdr bounds)))))
118
b578f267 119;; Go to beginning/end
1a2b6c52 120
c851323f
RS
121(defun beginning-of-thing (thing)
122 (let ((bounds (bounds-of-thing-at-point thing)))
123 (or bounds (error "No %s here" thing))
1a2b6c52
RS
124 (goto-char (car bounds))))
125
c851323f
RS
126(defun end-of-thing (thing)
127 (let ((bounds (bounds-of-thing-at-point thing)))
128 (or bounds (error "No %s here" thing))
1a2b6c52
RS
129 (goto-char (cdr bounds))))
130
b578f267 131;; Special cases
1a2b6c52 132
b578f267 133;; Lines
9f5c7ace
RS
134
135;; bolp will be false when you click on the last line in the buffer
136;; and it has no final newline.
137
138(put 'line 'beginning-op
139 (function (lambda () (if (bolp) (forward-line -1) (beginning-of-line)))))
140
b578f267 141;; Sexps
1a2b6c52
RS
142
143(defun in-string-p ()
144 (let ((orig (point)))
145 (save-excursion
146 (beginning-of-defun)
147 (nth 3 (parse-partial-sexp (point) orig)))))
148
149(defun end-of-sexp ()
150 (let ((char-syntax (char-syntax (char-after (point)))))
151 (if (or (eq char-syntax ?\))
152 (and (eq char-syntax ?\") (in-string-p)))
153 (forward-char 1)
154 (forward-sexp 1))))
155
156(put 'sexp 'end-op 'end-of-sexp)
157
b578f267 158;; Lists
1a2b6c52
RS
159
160(put 'list 'end-op (function (lambda () (up-list 1))))
161(put 'list 'beginning-op 'backward-sexp)
162
c851323f 163;; Filenames and URLs
1a2b6c52 164
c851323f 165(defvar thing-at-point-file-name-chars "~/A-Za-z0-9---_.${}#%,:"
1a2b6c52
RS
166 "Characters allowable in filenames.")
167
168(put 'filename 'end-op
c851323f 169 '(lambda () (skip-chars-forward thing-at-point-file-name-chars)))
1a2b6c52 170(put 'filename 'beginning-op
c851323f
RS
171 '(lambda () (skip-chars-backward thing-at-point-file-name-chars)))
172
02807c95 173(defvar thing-at-point-url-chars "~/A-Za-z0-9---_@$%&=.,"
c851323f
RS
174 "Characters allowable in a URL.")
175
176(put 'url 'end-op
02807c95
RS
177 '(lambda () (skip-chars-forward (concat ":" thing-at-point-url-chars))
178 (skip-chars-backward ".,:")))
c851323f
RS
179(put 'url 'beginning-op
180 '(lambda ()
181 (skip-chars-backward thing-at-point-url-chars)
182 (or (= (preceding-char) ?:)
183 (error "No URL here"))
184 (forward-char -1)
185 (skip-chars-backward "a-zA-Z")))
1a2b6c52 186
b578f267 187;; Whitespace
1a2b6c52 188
c851323f 189(defun forward-whitespace (arg)
1a2b6c52 190 (interactive "p")
c851323f
RS
191 (if (natnump arg)
192 (re-search-forward "[ \t]+\\|\n" nil nil arg)
193 (while (< arg 0)
1a2b6c52
RS
194 (if (re-search-backward "[ \t]+\\|\n" nil nil)
195 (or (eq (char-after (match-beginning 0)) 10)
196 (skip-chars-backward " \t")))
c851323f 197 (setq arg (1+ arg)))))
1a2b6c52 198
b578f267 199;; Buffer
1a2b6c52
RS
200
201(put 'buffer 'end-op 'end-of-buffer)
202(put 'buffer 'beginning-op 'beginning-of-buffer)
203
b578f267 204;; Symbols
1a2b6c52 205
c851323f 206(defun forward-symbol (arg)
1a2b6c52 207 (interactive "p")
c851323f
RS
208 (if (natnump arg)
209 (re-search-forward "\\(\\sw\\|\\s_\\)+" nil nil arg)
210 (while (< arg 0)
1a2b6c52
RS
211 (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil nil)
212 (skip-syntax-backward "w_"))
c851323f 213 (setq arg (1+ arg)))))
1a2b6c52 214
b578f267 215;; Syntax blocks
fde7326e
RS
216
217(defun forward-same-syntax (&optional arg)
218 (interactive "p")
219 (while (< arg 0)
220 (skip-syntax-backward
221 (char-to-string (char-syntax (char-after (1- (point))))))
222 (setq arg (1+ arg)))
223 (while (> arg 0)
224 (skip-syntax-forward (char-to-string (char-syntax (char-after (point)))))
225 (setq arg (1- arg))))
226
b578f267 227;; Aliases
1a2b6c52
RS
228
229(defun word-at-point () (thing-at-point 'word))
230(defun sentence-at-point () (thing-at-point 'sentence))
231
c851323f
RS
232(defun read-from-whole-string (str)
233 "Read a lisp expression from STR.
234Signal an error if the entire string was not used."
235 (let* ((read-data (read-from-string str))
1a2b6c52
RS
236 (more-left
237 (condition-case nil
c851323f 238 (progn (read-from-string (substring str (cdr read-data)))
1a2b6c52
RS
239 t)
240 (end-of-file nil))))
241 (if more-left
242 (error "Can't read whole string")
243 (car read-data))))
244
c851323f 245(defun form-at-point (&optional thing pred)
1a2b6c52 246 (let ((sexp (condition-case nil
c851323f 247 (read-from-whole-string (thing-at-point (or thing 'sexp)))
1a2b6c52 248 (error nil))))
c851323f 249 (if (or (not pred) (funcall pred sexp)) sexp)))
1a2b6c52
RS
250
251(defun sexp-at-point () (form-at-point 'sexp))
252(defun symbol-at-point () (form-at-point 'sexp 'symbolp))
253(defun number-at-point () (form-at-point 'sexp 'numberp))
254(defun list-at-point () (form-at-point 'list 'listp))
255
256;; thingatpt.el ends here.