comparison lisp/thingatpt.el @ 49597:e88404e8f2cf

Trailing whitespace deleted.
author Juanma Barranquero <lekktu@gmail.com>
date Tue, 04 Feb 2003 12:29:42 +0000
parents b8c5db4dbb2b
children 695cf19ef79e d7ddb3e565de
comparison
equal deleted inserted replaced
49596:b06535145619 49597:e88404e8f2cf
30 ;; positions by moving first forward to the end of the "thing", and then 30 ;; positions by moving first forward to the end of the "thing", and then
31 ;; backwards to the beginning. By default, it uses the corresponding 31 ;; backwards to the beginning. By default, it uses the corresponding
32 ;; forward-"thing" operator (eg. forward-word, forward-line). 32 ;; forward-"thing" operator (eg. forward-word, forward-line).
33 ;; 33 ;;
34 ;; Special cases are allowed for using properties associated with the named 34 ;; Special cases are allowed for using properties associated with the named
35 ;; "thing": 35 ;; "thing":
36 ;; 36 ;;
37 ;; forward-op Function to call to skip forward over a "thing" (or 37 ;; forward-op Function to call to skip forward over a "thing" (or
38 ;; with a negative argument, backward). 38 ;; with a negative argument, backward).
39 ;; 39 ;;
40 ;; beginning-op Function to call to skip to the beginning of a "thing". 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". 41 ;; end-op Function to call to skip to the end of a "thing".
42 ;; 42 ;;
43 ;; Reliance on existing operators means that many `things' can be accessed 43 ;; Reliance on existing operators means that many `things' can be accessed
44 ;; without further code: eg. 44 ;; without further code: eg.
78 (funcall (get thing 'bounds-of-thing-at-point)) 78 (funcall (get thing 'bounds-of-thing-at-point))
79 (let ((orig (point))) 79 (let ((orig (point)))
80 (condition-case nil 80 (condition-case nil
81 (save-excursion 81 (save-excursion
82 ;; Try moving forward, then back. 82 ;; Try moving forward, then back.
83 (let ((end (progn 83 (let ((end (progn
84 (funcall 84 (funcall
85 (or (get thing 'end-op) 85 (or (get thing 'end-op)
86 (function (lambda () (forward-thing thing 1))))) 86 (function (lambda () (forward-thing thing 1)))))
87 (point))) 87 (point)))
88 (beg (progn 88 (beg (progn
89 (funcall 89 (funcall
90 (or (get thing 'beginning-op) 90 (or (get thing 'beginning-op)
91 (function (lambda () (forward-thing thing -1))))) 91 (function (lambda () (forward-thing thing -1)))))
92 (point)))) 92 (point))))
93 (if (not (and beg (> beg orig))) 93 (if (not (and beg (> beg orig)))
94 ;; If that brings us all the way back to ORIG, 94 ;; If that brings us all the way back to ORIG,
95 ;; it worked. But END may not be the real end. 95 ;; it worked. But END may not be the real end.
96 ;; So find the real end that corresponds to BEG. 96 ;; So find the real end that corresponds to BEG.
97 (let ((real-end 97 (let ((real-end
98 (progn 98 (progn
99 (funcall 99 (funcall
100 (or (get thing 'end-op) 100 (or (get thing 'end-op)
101 (function (lambda () (forward-thing thing 1))))) 101 (function (lambda () (forward-thing thing 1)))))
102 (point)))) 102 (point))))
103 (if (and beg real-end (<= beg orig) (<= orig real-end)) 103 (if (and beg real-end (<= beg orig) (<= orig real-end))
104 (cons beg real-end))) 104 (cons beg real-end)))
105 (goto-char orig) 105 (goto-char orig)
106 ;; Try a second time, moving backward first and then forward, 106 ;; Try a second time, moving backward first and then forward,
107 ;; so that we can find a thing that ends at ORIG. 107 ;; so that we can find a thing that ends at ORIG.
108 (let ((beg (progn 108 (let ((beg (progn
109 (funcall 109 (funcall
110 (or (get thing 'beginning-op) 110 (or (get thing 'beginning-op)
111 (function (lambda () (forward-thing thing -1))))) 111 (function (lambda () (forward-thing thing -1)))))
112 (point))) 112 (point)))
113 (end (progn 113 (end (progn
114 (funcall 114 (funcall
115 (or (get thing 'end-op) 115 (or (get thing 'end-op)
116 (function (lambda () (forward-thing thing 1))))) 116 (function (lambda () (forward-thing thing 1)))))
117 (point))) 117 (point)))
118 (real-beg 118 (real-beg
119 (progn 119 (progn
120 (funcall 120 (funcall
121 (or (get thing 'beginning-op) 121 (or (get thing 'beginning-op)
122 (function (lambda () (forward-thing thing -1))))) 122 (function (lambda () (forward-thing thing -1)))))
123 (point)))) 123 (point))))
124 (if (and real-beg end (<= real-beg orig) (<= orig end)) 124 (if (and real-beg end (<= real-beg orig) (<= orig end))
125 (cons real-beg end)))))) 125 (cons real-beg end))))))
126 (error nil))))) 126 (error nil)))))
135 See the file `thingatpt.el' for documentation on how to define 135 See the file `thingatpt.el' for documentation on how to define
136 a symbol as a valid THING." 136 a symbol as a valid THING."
137 (if (get thing 'thing-at-point) 137 (if (get thing 'thing-at-point)
138 (funcall (get thing 'thing-at-point)) 138 (funcall (get thing 'thing-at-point))
139 (let ((bounds (bounds-of-thing-at-point thing))) 139 (let ((bounds (bounds-of-thing-at-point thing)))
140 (if bounds 140 (if bounds
141 (buffer-substring (car bounds) (cdr bounds)))))) 141 (buffer-substring (car bounds) (cdr bounds))))))
142 142
143 ;; Go to beginning/end 143 ;; Go to beginning/end
144 144
145 (defun beginning-of-thing (thing) 145 (defun beginning-of-thing (thing)
150 (defun end-of-thing (thing) 150 (defun end-of-thing (thing)
151 (let ((bounds (bounds-of-thing-at-point thing))) 151 (let ((bounds (bounds-of-thing-at-point thing)))
152 (or bounds (error "No %s here" thing)) 152 (or bounds (error "No %s here" thing))
153 (goto-char (cdr bounds)))) 153 (goto-char (cdr bounds))))
154 154
155 ;; Special cases 155 ;; Special cases
156 156
157 ;; Lines 157 ;; Lines
158 158
159 ;; bolp will be false when you click on the last line in the buffer 159 ;; bolp will be false when you click on the last line in the buffer
160 ;; and it has no final newline. 160 ;; and it has no final newline.
161 161
162 (put 'line 'beginning-op 162 (put 'line 'beginning-op
163 (function (lambda () (if (bolp) (forward-line -1) (beginning-of-line))))) 163 (function (lambda () (if (bolp) (forward-line -1) (beginning-of-line)))))
164 164
165 ;; Sexps 165 ;; Sexps
166 166
167 (defun in-string-p () 167 (defun in-string-p ()
168 (let ((orig (point))) 168 (let ((orig (point)))
169 (save-excursion 169 (save-excursion
170 (beginning-of-defun) 170 (beginning-of-defun)
186 (forward-char -1) 186 (forward-char -1)
187 (forward-sexp -1)))) 187 (forward-sexp -1))))
188 188
189 (put 'sexp 'beginning-op 'beginning-of-sexp) 189 (put 'sexp 'beginning-op 'beginning-of-sexp)
190 190
191 ;; Lists 191 ;; Lists
192 192
193 (put 'list 'end-op (function (lambda () (up-list 1)))) 193 (put 'list 'end-op (function (lambda () (up-list 1))))
194 (put 'list 'beginning-op 'backward-sexp) 194 (put 'list 'beginning-op 'backward-sexp)
195 195
196 ;; Filenames and URLs www.com/foo%32bar 196 ;; Filenames and URLs www.com/foo%32bar
197 197
198 (defvar thing-at-point-file-name-chars "-~/[:alnum:]_.${}#%,:" 198 (defvar thing-at-point-file-name-chars "-~/[:alnum:]_.${}#%,:"
199 "Characters allowable in filenames.") 199 "Characters allowable in filenames.")
200 200
201 (put 'filename 'end-op 201 (put 'filename 'end-op
202 (lambda () 202 (lambda ()
203 (re-search-forward (concat "\\=[" thing-at-point-file-name-chars "]*") 203 (re-search-forward (concat "\\=[" thing-at-point-file-name-chars "]*")
204 nil t))) 204 nil t)))
205 (put 'filename 'beginning-op 205 (put 'filename 'beginning-op
206 (lambda () 206 (lambda ()
337 (let ((bounds (thing-at-point-bounds-of-url-at-point))) 337 (let ((bounds (thing-at-point-bounds-of-url-at-point)))
338 (if bounds 338 (if bounds
339 (goto-char (car bounds)) 339 (goto-char (car bounds))
340 (error "No URL here")))))) 340 (error "No URL here"))))))
341 341
342 ;; Whitespace 342 ;; Whitespace
343 343
344 (defun forward-whitespace (arg) 344 (defun forward-whitespace (arg)
345 (interactive "p") 345 (interactive "p")
346 (if (natnump arg) 346 (if (natnump arg)
347 (re-search-forward "[ \t]+\\|\n" nil 'move arg) 347 (re-search-forward "[ \t]+\\|\n" nil 'move arg)
348 (while (< arg 0) 348 (while (< arg 0)
349 (if (re-search-backward "[ \t]+\\|\n" nil 'move) 349 (if (re-search-backward "[ \t]+\\|\n" nil 'move)
350 (or (eq (char-after (match-beginning 0)) 10) 350 (or (eq (char-after (match-beginning 0)) 10)
351 (skip-chars-backward " \t"))) 351 (skip-chars-backward " \t")))
352 (setq arg (1+ arg))))) 352 (setq arg (1+ arg)))))
353 353
354 ;; Buffer 354 ;; Buffer
355 355
356 (put 'buffer 'end-op (lambda () (goto-char (point-max)))) 356 (put 'buffer 'end-op (lambda () (goto-char (point-max))))
357 (put 'buffer 'beginning-op (lambda () (goto-char (point-min)))) 357 (put 'buffer 'beginning-op (lambda () (goto-char (point-min))))
358 358
359 ;; Symbols 359 ;; Symbols
360 360
361 (defun forward-symbol (arg) 361 (defun forward-symbol (arg)
362 (interactive "p") 362 (interactive "p")
363 (if (natnump arg) 363 (if (natnump arg)
364 (re-search-forward "\\(\\sw\\|\\s_\\)+" nil 'move arg) 364 (re-search-forward "\\(\\sw\\|\\s_\\)+" nil 'move arg)
365 (while (< arg 0) 365 (while (< arg 0)
366 (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil 'move) 366 (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil 'move)
367 (skip-syntax-backward "w_")) 367 (skip-syntax-backward "w_"))
368 (setq arg (1+ arg))))) 368 (setq arg (1+ arg)))))
369 369
370 ;; Syntax blocks 370 ;; Syntax blocks
371 371
372 (defun forward-same-syntax (&optional arg) 372 (defun forward-same-syntax (&optional arg)
373 (interactive "p") 373 (interactive "p")
374 (while (< arg 0) 374 (while (< arg 0)
375 (skip-syntax-backward 375 (skip-syntax-backward
376 (char-to-string (char-syntax (char-after (1- (point)))))) 376 (char-to-string (char-syntax (char-after (1- (point))))))
377 (setq arg (1+ arg))) 377 (setq arg (1+ arg)))
378 (while (> arg 0) 378 (while (> arg 0)
379 (skip-syntax-forward (char-to-string (char-syntax (char-after (point))))) 379 (skip-syntax-forward (char-to-string (char-syntax (char-after (point)))))
380 (setq arg (1- arg)))) 380 (setq arg (1- arg))))
381 381
382 ;; Aliases 382 ;; Aliases
383 383
384 (defun word-at-point () (thing-at-point 'word)) 384 (defun word-at-point () (thing-at-point 'word))
385 (defun sentence-at-point () (thing-at-point 'sentence)) 385 (defun sentence-at-point () (thing-at-point 'sentence))
386 386
387 (defun read-from-whole-string (str) 387 (defun read-from-whole-string (str)
388 "Read a lisp expression from STR. 388 "Read a lisp expression from STR.
389 Signal an error if the entire string was not used." 389 Signal an error if the entire string was not used."
390 (let* ((read-data (read-from-string str)) 390 (let* ((read-data (read-from-string str))
391 (more-left 391 (more-left
392 (condition-case nil 392 (condition-case nil
393 ;; The call to `ignore' suppresses a compiler warning. 393 ;; The call to `ignore' suppresses a compiler warning.
394 (progn (ignore (read-from-string (substring str (cdr read-data)))) 394 (progn (ignore (read-from-string (substring str (cdr read-data))))
395 t) 395 t)
396 (end-of-file nil)))) 396 (end-of-file nil))))
397 (if more-left 397 (if more-left
398 (error "Can't read whole string") 398 (error "Can't read whole string")
399 (car read-data)))) 399 (car read-data))))
400 400
401 (defun form-at-point (&optional thing pred) 401 (defun form-at-point (&optional thing pred)
402 (let ((sexp (condition-case nil 402 (let ((sexp (condition-case nil
403 (read-from-whole-string (thing-at-point (or thing 'sexp))) 403 (read-from-whole-string (thing-at-point (or thing 'sexp)))
404 (error nil)))) 404 (error nil))))
405 (if (or (not pred) (funcall pred sexp)) sexp))) 405 (if (or (not pred) (funcall pred sexp)) sexp)))
406 406
407 ;;;###autoload 407 ;;;###autoload