0
|
1 ;;; thing.el --- find language-specific contiguous pieces of text
|
|
2
|
|
3 ;; Keywords: extensions, languages
|
|
4
|
|
5 ;;; Authors: David Hughes <djh@cis.prime.com>
|
|
6 ;;; adapted from Martin Boyer's thing.el for imouse
|
|
7 ;;; Martin Boyer, IREQ <mboyer@ireq-robot.hydro.qc.ca>
|
|
8 ;;; adapted from Heinz Schmidt's thing.el for sky-mouse
|
|
9 ;;; Heinz Schmidt, ICSI (hws@ICSI.Berkeley.EDU)
|
|
10 ;;; adapted from Dan L. Pierson's epoch-thing.el
|
|
11 ;;; Dan L. Pierson <pierson@encore.com>, 2/5/90
|
|
12 ;;; adapted from Joshua Guttman's Thing.el
|
|
13 ;;; Joshua Guttman, MITRE (guttman@mitre.org)
|
|
14 ;;; adapted from sun-fns.el by Joshua Guttman, MITRE.
|
|
15 ;;;
|
|
16 ;;; Copyright (C) International Computer Science Institute, 1991
|
|
17 ;;;
|
|
18
|
|
19 ;; This file is part of XEmacs.
|
|
20
|
|
21 ;; XEmacs is free software; you can redistribute it and/or modify
|
|
22 ;; it under the terms of the GNU General Public License as published by
|
|
23 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
24 ;; any later version.
|
|
25
|
|
26 ;; XEmacs is distributed in the hope that it will be useful,
|
|
27 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
28 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
29 ;; GNU General Public License for more details.
|
|
30
|
|
31 ;; You should have received a copy of the GNU General Public License
|
12
|
32 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
33 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
34 ;; 02111-1307, USA.
|
0
|
35
|
|
36 ;;; Synched up with: Not in FSF.
|
|
37 ;;; #### FSF has thingatpt.el, which does the same thing. Should merge
|
|
38 ;;; or toss this.
|
|
39
|
|
40 ;;;*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
41 ;;;* FUNCTION: Things are language objects contiguous pieces of text
|
|
42 ;;;* whose boundaries can be defined by syntax or context.
|
|
43 ;;;*
|
|
44 ;;;* RELATED PACKAGES: various packages built on this.
|
|
45 ;;;*
|
|
46 ;;;* HISTORY:
|
|
47 ;;;* Last edited: David Hughes 21st December 1992
|
|
48 ;;;* jul 21 21:00 1993 (tlp00): added a kludgy thing-filename
|
|
49 ;;;* Feb 22 21:00 1993 (tlp00): better merge with lucid and imouse
|
|
50 ;;;* Dec 21 11:11 1992 (djh): added thing-report-char-p
|
|
51 ;;;* Nov 23 18:00 1992 (djh): merged in Guido Bosch's ideas
|
|
52 ;;;* Sep 10 15:35 1992 (djh): adapted for Lucid emacs19-mouse.el
|
|
53 ;;;* Nov 28 17:40 1991 (mb): Cleaned up, and added thing-bigger-alist.
|
|
54 ;;;* May 24 00:33 1991 (hws): overworked and added syntax.
|
|
55 ;;;* Created: 2/5/90 Dan L. Pierson
|
|
56 ;;;*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
57
|
|
58 (provide 'thing)
|
|
59
|
|
60 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
61 ;;;;;;;;;;;; Customization and Entry Point ;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
62 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
63
|
|
64 (defvar thing-boundary-alist
|
|
65 '((?w thing-word)
|
|
66 (?_ thing-symbol)
|
|
67 (?\( thing-sexp-start)
|
|
68 (?\$ thing-sexp-start)
|
|
69 (?' thing-sexp-start)
|
|
70 (?\" thing-sexp-start)
|
|
71 (?\) thing-sexp-end)
|
|
72 (? thing-whitespace)
|
|
73 (?< thing-comment)
|
|
74 (?. thing-next-sexp))
|
|
75 "*List of pairs of the form (SYNTAX-CHAR FUNCTION) used by
|
|
76 the function `thing-boundaries'.")
|
|
77
|
|
78 (defvar thing-report-char-p t
|
|
79 "*Non nil means return single char boundaries if all else fails")
|
|
80
|
|
81 (defvar thing-report-whitespace t
|
|
82 "*Non nil means that whitespaces are considered as things, otherwise not.")
|
|
83
|
|
84 (defvar *last-thing*
|
|
85 "The last thing found by thing-boundaries. Used for chaining commands.")
|
|
86
|
|
87 ;; The variable and function `thing-region' are to avoid the continual
|
|
88 ;; construction of cons cells as result af the thing scanner functions.
|
|
89 ;; This avoids unnecessary garbage collection. Guido Bosch <bosch@loria.fr>
|
|
90
|
|
91 (defvar thing-region (cons 'nil 'nil)
|
|
92 "Cons cell that contains a region (<beginning> . <end>)
|
|
93 The function `thing-region' updates and returns it.")
|
|
94
|
|
95 (defun thing-region (beginning end)
|
|
96 "Make BEGINNING the car and END the cdr of the cons cell in the
|
|
97 variable `thing-region'. Return the updated cons cell"
|
|
98 (cond ((/= beginning end)
|
|
99 (setcar thing-region beginning)
|
|
100 (setcdr thing-region end)
|
|
101 thing-region)))
|
|
102
|
|
103 (defvar thing-bigger-alist
|
|
104 '((word-symbol thing-symbol)
|
|
105 (symbol thing-sexp)
|
|
106 (word-sexp thing-sexp)
|
|
107 (sexp thing-up-sexp)
|
|
108 (sexp-up thing-up-sexp)
|
|
109 (line thing-paragraph)
|
|
110 (paragraph thing-page)
|
|
111 (char thing-word)
|
|
112 (word-sentence thing-sentence)
|
|
113 (sentence thing-paragraph))
|
|
114 "List of pairs to go from one thing to a bigger thing.
|
|
115 See mouse-select-bigger-thing and mouse-delete-bigger-thing.")
|
|
116
|
|
117 (defvar thing-word-next nil
|
|
118 "*The next bigger thing after a word. A symbol.
|
|
119 Supported values are: word-symbol, word-sexp, and word-sentence.
|
|
120 Default value is word-sentence.
|
|
121 Automatically becomes local when set in any fashion.")
|
|
122 (make-variable-buffer-local 'thing-word-next)
|
|
123
|
|
124 (defun thing-boundaries (here)
|
|
125 "Return start and end of text object at HERE using syntax table and
|
|
126 thing-boundary-alist. Thing-boundary-alist is a list of pairs of the
|
|
127 form (SYNTAX-CHAR FUNCTION) where FUNCTION takes a single position
|
|
128 argument and returns a cons of places (start end) representing
|
|
129 boundaries of the thing at that position.
|
|
130
|
|
131 Typically:
|
|
132 Left or right Paren syntax indicates an s-expression.
|
|
133 The end of a line marks the line including a trailing newline.
|
|
134 Word syntax indicates current word.
|
|
135 Symbol syntax indicates symbol.
|
|
136 If it doesn't recognize one of these it selects just the character HERE.
|
|
137
|
|
138 If an error occurs during syntax scanning, the function just prints a
|
|
139 message and returns `nil'."
|
|
140 (interactive "d")
|
|
141 (setq *last-thing* nil)
|
|
142 (if (save-excursion (goto-char here) (eolp))
|
|
143 (thing-get-line here)
|
|
144 (let* ((syntax (char-syntax (char-after here)))
|
|
145 (pair (assq syntax thing-boundary-alist)))
|
|
146 (cond ((and pair
|
|
147 (or thing-report-whitespace
|
|
148 (not (eq (car (cdr pair)) 'thing-whitespace))))
|
|
149 (funcall (car (cdr pair)) here))
|
|
150 (thing-report-char-p
|
|
151 (setq *last-thing* 'char)
|
|
152 (thing-region here (1+ here)))
|
|
153 (t
|
|
154 nil)))))
|
|
155
|
|
156
|
|
157
|
|
158
|
|
159 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
160 ;;;;;;;;;;;;;;;;; Code Delimiters ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
161 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
162
|
|
163 (defun thing-symbol (here)
|
|
164 "Return start and end of symbol at HERE."
|
|
165 (cond ((memq (char-syntax (char-after here)) '(?_ ?w))
|
|
166 (setq *last-thing* 'symbol)
|
|
167 (let ((end (scan-sexps here 1)))
|
|
168 (thing-region (min here (scan-sexps end -1)) end)))))
|
|
169
|
|
170 (defun thing-filename (here)
|
|
171 "Return start and end of filename at HERE."
|
|
172 (cond ((memq (char-syntax (char-after here)) '(?w ?_ ?.))
|
|
173 (let (start end)
|
|
174 (save-excursion
|
|
175 (goto-char here)
|
|
176 (and (re-search-forward "\\s \\|:\\s\"\\|$" nil t)
|
|
177 (goto-char (setq end (match-beginning 0)))
|
|
178 (or
|
|
179 (and
|
|
180 (re-search-backward "[^_a-zA-Z0-9---#$.~/@]+" nil t)
|
|
181 (setq start (+ (match-beginning 0)
|
|
182 (if (bolp)
|
|
183 0
|
|
184 1))))
|
|
185 (setq start (point-min)))
|
|
186 (thing-region (min start here) (max here end))))))))
|
|
187 ;~/
|
|
188 (defun thing-sexp-start (here)
|
|
189 "Return start and end of sexp starting HERE."
|
|
190 (setq *last-thing* 'sexp-start)
|
|
191 (thing-region here (scan-sexps here 1)))
|
|
192
|
|
193 (defun thing-sexp-end (here)
|
|
194 "Return start and end of sexp ending HERE."
|
|
195 (setq *last-thing* 'sexp-end)
|
|
196 (thing-region (scan-sexps (1+ here) -1) (1+ here)))
|
|
197
|
|
198 (defun thing-sexp (here)
|
|
199 "Return start and end of the sexp at HERE."
|
|
200 (setq *last-thing* 'sexp)
|
|
201 (save-excursion
|
|
202 (goto-char here)
|
|
203 (thing-region (progn (backward-up-list 1) (point))
|
|
204 (progn (forward-list 1) (point)))))
|
|
205
|
|
206 (defun thing-up-sexp (here)
|
|
207 "Return start and end of the sexp enclosing the selected area."
|
|
208 (setq *last-thing* 'sexp-up)
|
|
209 ;; Keep going up and backward in sexps. This means that thing-up-sexp
|
|
210 ;; can only be called after thing-sexp or after itself.
|
|
211 (save-excursion
|
|
212 (goto-char here)
|
|
213 (thing-region (progn
|
|
214 (condition-case ()
|
|
215 (backward-up-list 1) (error nil))
|
|
216 (point))
|
|
217 (progn
|
|
218 (condition-case ()
|
|
219 (forward-list 1) (error nil))
|
|
220 (point)))))
|
|
221
|
|
222 ;;; Allow punctuation marks not followed by white-space to include
|
|
223 ;;; the subsequent sexp. Useful in foo.bar(x).baz and such.
|
|
224 (defun thing-next-sexp (here)
|
|
225 "Return from HERE to the end of the sexp at HERE,
|
|
226 if the character at HERE is part of a sexp."
|
|
227 (setq *last-thing* 'sexp-next)
|
|
228 (if (= (char-syntax (char-after (1+ here))) ? )
|
|
229 (thing-region here (1+ here))
|
|
230 (thing-region here
|
|
231 (save-excursion (goto-char here) (forward-sexp) (point)))))
|
|
232
|
|
233 ;;; Allow click to comment-char to extend to end of line
|
|
234 (defun thing-comment (here)
|
|
235 "Return rest of line from HERE to newline."
|
|
236 (setq *last-thing* 'comment)
|
|
237 (save-excursion (goto-char here)
|
|
238 (while (= (char-syntax (preceding-char)) ?<)
|
|
239 (forward-char -1))
|
|
240 (thing-region (point) (progn (end-of-line) (point)))))
|
|
241
|
|
242
|
|
243 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
244 ;;;;;;;;;;;;;;;;; Text Delimiters ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
245 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
246
|
|
247 (defun thing-word (here)
|
|
248 "Return start and end of word at HERE."
|
|
249 (setq *last-thing*
|
|
250 (if thing-word-next
|
|
251 thing-word-next
|
|
252 (setq thing-word-next
|
|
253 (cond
|
|
254 ((memq major-mode '(emacs-lisp-mode c-mode c++-mode
|
|
255 fortran-mode latex-mode lisp-mode
|
|
256 perl-mode tex-mode))
|
|
257 'word-symbol)
|
|
258 (t 'word-sentence)))))
|
|
259 (save-excursion
|
|
260 (goto-char here)
|
|
261 (forward-word 1)
|
|
262 (let ((end (point)))
|
|
263 (forward-word -1)
|
|
264 (thing-region (point) end))))
|
|
265
|
|
266 (defun thing-sentence (here)
|
|
267 "Return start and end of the sentence at HERE."
|
|
268 (setq *last-thing* 'sentence)
|
|
269 (save-excursion
|
|
270 (goto-char here)
|
|
271 (thing-region (progn (backward-sentence) (point))
|
|
272 (progn (forward-sentence) (point)))))
|
|
273
|
|
274 (defun thing-whitespace (here)
|
|
275 "Return start to end of all of whitespace HERE."
|
|
276 (setq *last-thing* 'whitespace)
|
|
277 (save-excursion
|
|
278 (goto-char here)
|
|
279 (let ((start (progn (skip-chars-backward " \t") (1+ (point))))
|
|
280 (end (progn (skip-chars-forward " \t") (point))))
|
|
281 (if (= start end)
|
|
282 (thing-region (1- start) end)
|
|
283 (thing-region start end)))))
|
|
284
|
|
285
|
|
286 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
287 ;;;;;;;;;;;;;;; Physical Delimiters ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
288 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
289
|
|
290 (defun thing-get-line (here)
|
|
291 "Return whole of line HERE is in, with newline unless at eob."
|
|
292 (setq *last-thing* 'line)
|
|
293 (save-excursion
|
|
294 (goto-char here)
|
|
295 (let* ((start (progn (beginning-of-line 1) (point))))
|
|
296 (thing-region start (point)))))
|
|
297
|
|
298 (defun thing-paragraph (here)
|
|
299 "Return start and end of the paragraph at HERE."
|
|
300 (setq *last-thing* 'paragraph)
|
|
301 (save-excursion
|
|
302 (goto-char here)
|
|
303 (thing-region (progn (backward-paragraph) (point))
|
|
304 (progn (forward-paragraph) (point)))))
|
|
305
|
|
306 (defun thing-page (here)
|
|
307 "Return start and end of the page at HERE."
|
|
308 (setq *last-thing* 'page)
|
|
309 (save-excursion
|
|
310 (goto-char here)
|
|
311 (thing-region (progn (backward-page) (point))
|
|
312 (progn (forward-page) (point)))))
|
|
313
|
|
314
|
|
315 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
316 ;;;;;;;;;;;;;;;; Support functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
317 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
318
|
|
319 (defun kill-thing-at-point (here)
|
|
320 "Kill text object using syntax table.
|
|
321 See thing-boundaries for definition of text objects"
|
|
322 (interactive "d")
|
|
323 (let ((bounds (thing-boundaries here)))
|
|
324 (kill-region (car bounds) (cdr bounds))))
|
|
325
|
|
326 (defun copy-thing-at-point (here)
|
|
327 "Copy text object using syntax table.
|
|
328 See thing-boundaries for definition of text objects"
|
|
329 (interactive "d")
|
|
330 (let ((bounds (thing-boundaries here)))
|
|
331 (copy-region-as-kill (car bounds) (cdr bounds))))
|