annotate lisp/thingatpt.el @ 16773:7064fa2cb0d7

(dired-insert-directory): Don't override the user's locale. (dired-standard-move-to-filename-regexp): Var deleted.
author Richard M. Stallman <rms@gnu.org>
date Sun, 29 Dec 1996 08:14:57 +0000
parents bf249c4b4beb
children 3ae7560f0959
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1 ;;; thingatpt.el --- Get the `thing' at point
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
3 ;; Copyright (C) 1991,92,93,94,95,1996 Free Software Foundation, Inc.
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
4
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5 ;; Author: Mike Williams <mikew@gopher.dosli.govt.nz>
5140
9cde7d7fea1f Comment change.
Richard M. Stallman <rms@gnu.org>
parents: 4934
diff changeset
6 ;; Keywords: extensions, matching, mouse
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
7 ;; Created: Thu Mar 28 13:48:23 1991
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9 ;; This file is part of GNU Emacs.
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
14 ;; any later version.
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19 ;; GNU General Public License for more details.
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20
7938
c0cc87942423 Comment change.
Richard M. Stallman <rms@gnu.org>
parents: 5751
diff changeset
21 ;;; Commentary:
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14010
diff changeset
22
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
23 ;; This file provides routines for getting the "thing" at the location of
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
24 ;; point, whatever that "thing" happens to be. The "thing" is defined by
16427
3b9f64eb097b Comment change.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
25 ;; its beginning and end positions in the buffer.
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
26 ;;
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27 ;; The function bounds-of-thing-at-point finds the beginning and end
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
28 ;; positions by moving first forward to the end of the "thing", and then
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
29 ;; backwards to the beginning. By default, it uses the corresponding
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
30 ;; forward-"thing" operator (eg. forward-word, forward-line).
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31 ;;
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
32 ;; Special cases are allowed for using properties associated with the named
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
33 ;; "thing":
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
34 ;;
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
35 ;; forward-op Function to call to skip forward over a "thing" (or
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
36 ;; with a negative argument, backward).
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
37 ;;
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
38 ;; beginning-op Function to call to skip to the beginning of a "thing".
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
39 ;; end-op Function to call to skip to the end of a "thing".
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
40 ;;
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
41 ;; Reliance on existing operators means that many `things' can be accessed
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
42 ;; without further code: eg.
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43 ;; (thing-at-point 'line)
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44 ;; (thing-at-point 'page)
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14010
diff changeset
46 ;;; Code:
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48 (provide 'thingatpt)
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14010
diff changeset
50 ;; Basic movement
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 ;;;###autoload
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
53 (defun forward-thing (thing &optional n)
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54 "Move forward to the end of the next THING."
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
55 (let ((forward-op (or (get thing 'forward-op)
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
56 (intern-soft (format "forward-%s" thing)))))
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57 (if (fboundp forward-op)
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
58 (funcall forward-op (or n 1))
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
59 (error "Can't determine how to move over a %s" thing))))
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14010
diff changeset
61 ;; General routines
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 ;;;###autoload
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
64 (defun bounds-of-thing-at-point (thing)
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
65 "Determine the start and end buffer locations for the THING at point.
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
66 THING is a symbol which specifies the kind of syntactic entity you want.
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
67 Possibilities include `symbol', `list', `sexp', `defun', `filename', `url',
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
68 `word', `sentence', `whitespace', `line', `page' and others.
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
69
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
70 See the file `thingatpt.el' for documentation on how to define
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
71 a symbol as a valid THING.
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
72
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
73 The value is a cons cell (START . END) giving the start and end positions
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
74 of the textual entity that was found."
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75 (let ((orig (point)))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76 (condition-case nil
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
77 (save-excursion
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
78 (let ((end (progn
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
79 (funcall
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
80 (or (get thing 'end-op)
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
81 (function (lambda () (forward-thing thing 1)))))
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
82 (point)))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
83 (beg (progn
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
84 (funcall
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
85 (or (get thing 'beginning-op)
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
86 (function (lambda () (forward-thing thing -1)))))
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
87 (point))))
16668
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
88 (if (and beg end (<= beg orig) (<= orig end))
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
89 (cons beg end)
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
90 ;; Try a second time, moving backward first and forward after,
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
91 ;; so that we can find a thing that ends at ORIG.
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
92 (let ((beg (progn
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
93 (funcall
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
94 (or (get thing 'beginning-op)
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
95 (function (lambda () (forward-thing thing -1)))))
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
96 (point)))
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
97 (end (progn
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
98 (funcall
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
99 (or (get thing 'end-op)
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
100 (function (lambda () (forward-thing thing 1)))))
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
101 (point))))
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
102 (if (and beg end (<= beg orig) (<= orig end))
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
103 (cons beg end))))))
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
104 (error nil))))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
106 ;;;###autoload
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
107 (defun thing-at-point (thing)
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
108 "Return the THING at point.
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
109 THING is a symbol which specifies the kind of syntactic entity you want.
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
110 Possibilities include `symbol', `list', `sexp', `defun', `filename', `url',
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
111 `word', `sentence', `whitespace', `line', `page' and others.
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
112
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
113 See the file `thingatpt.el' for documentation on how to define
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
114 a symbol as a valid THING."
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
115 (let ((bounds (bounds-of-thing-at-point thing)))
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
116 (if bounds
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117 (buffer-substring (car bounds) (cdr bounds)))))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14010
diff changeset
119 ;; Go to beginning/end
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
121 (defun beginning-of-thing (thing)
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
122 (let ((bounds (bounds-of-thing-at-point thing)))
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
123 (or bounds (error "No %s here" thing))
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124 (goto-char (car bounds))))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
125
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
126 (defun end-of-thing (thing)
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
127 (let ((bounds (bounds-of-thing-at-point thing)))
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
128 (or bounds (error "No %s here" thing))
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129 (goto-char (cdr bounds))))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14010
diff changeset
131 ;; Special cases
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
132
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14010
diff changeset
133 ;; Lines
9931
23e429e3fb18 (line): Add a beginning-op function.
Richard M. Stallman <rms@gnu.org>
parents: 7938
diff changeset
134
23e429e3fb18 (line): Add a beginning-op function.
Richard M. Stallman <rms@gnu.org>
parents: 7938
diff changeset
135 ;; bolp will be false when you click on the last line in the buffer
23e429e3fb18 (line): Add a beginning-op function.
Richard M. Stallman <rms@gnu.org>
parents: 7938
diff changeset
136 ;; and it has no final newline.
23e429e3fb18 (line): Add a beginning-op function.
Richard M. Stallman <rms@gnu.org>
parents: 7938
diff changeset
137
23e429e3fb18 (line): Add a beginning-op function.
Richard M. Stallman <rms@gnu.org>
parents: 7938
diff changeset
138 (put 'line 'beginning-op
23e429e3fb18 (line): Add a beginning-op function.
Richard M. Stallman <rms@gnu.org>
parents: 7938
diff changeset
139 (function (lambda () (if (bolp) (forward-line -1) (beginning-of-line)))))
23e429e3fb18 (line): Add a beginning-op function.
Richard M. Stallman <rms@gnu.org>
parents: 7938
diff changeset
140
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14010
diff changeset
141 ;; Sexps
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 (defun in-string-p ()
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144 (let ((orig (point)))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145 (save-excursion
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146 (beginning-of-defun)
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 (nth 3 (parse-partial-sexp (point) orig)))))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149 (defun end-of-sexp ()
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150 (let ((char-syntax (char-syntax (char-after (point)))))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 (if (or (eq char-syntax ?\))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 (and (eq char-syntax ?\") (in-string-p)))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153 (forward-char 1)
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 (forward-sexp 1))))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156 (put 'sexp 'end-op 'end-of-sexp)
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14010
diff changeset
158 ;; Lists
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 (put 'list 'end-op (function (lambda () (up-list 1))))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161 (put 'list 'beginning-op 'backward-sexp)
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
163 ;; Filenames and URLs
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
165 (defvar thing-at-point-file-name-chars "~/A-Za-z0-9---_.${}#%,:"
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 "Characters allowable in filenames.")
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 (put 'filename 'end-op
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
169 '(lambda () (skip-chars-forward thing-at-point-file-name-chars)))
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170 (put 'filename 'beginning-op
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
171 '(lambda () (skip-chars-backward thing-at-point-file-name-chars)))
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
172
16668
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
173 (defvar thing-at-point-url-chars "~/A-Za-z0-9---_@$%&=.,"
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
174 "Characters allowable in a URL.")
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
175
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
176 (put 'url 'end-op
16668
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
177 '(lambda () (skip-chars-forward (concat ":" thing-at-point-url-chars))
bf249c4b4beb (bounds-of-thing-at-point): Allow the end
Richard M. Stallman <rms@gnu.org>
parents: 16653
diff changeset
178 (skip-chars-backward ".,:")))
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
179 (put 'url 'beginning-op
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
180 '(lambda ()
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
181 (skip-chars-backward thing-at-point-url-chars)
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
182 (or (= (preceding-char) ?:)
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
183 (error "No URL here"))
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
184 (forward-char -1)
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
185 (skip-chars-backward "a-zA-Z")))
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
186
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14010
diff changeset
187 ;; Whitespace
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
188
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
189 (defun forward-whitespace (arg)
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
190 (interactive "p")
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
191 (if (natnump arg)
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
192 (re-search-forward "[ \t]+\\|\n" nil nil arg)
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
193 (while (< arg 0)
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194 (if (re-search-backward "[ \t]+\\|\n" nil nil)
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195 (or (eq (char-after (match-beginning 0)) 10)
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196 (skip-chars-backward " \t")))
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
197 (setq arg (1+ arg)))))
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14010
diff changeset
199 ;; Buffer
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
200
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
201 (put 'buffer 'end-op 'end-of-buffer)
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202 (put 'buffer 'beginning-op 'beginning-of-buffer)
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
203
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14010
diff changeset
204 ;; Symbols
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
206 (defun forward-symbol (arg)
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
207 (interactive "p")
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
208 (if (natnump arg)
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
209 (re-search-forward "\\(\\sw\\|\\s_\\)+" nil nil arg)
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
210 (while (< arg 0)
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
211 (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil nil)
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
212 (skip-syntax-backward "w_"))
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
213 (setq arg (1+ arg)))))
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
214
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14010
diff changeset
215 ;; Syntax blocks
12593
e961f9a213a7 (forward-same-syntax): New function.
Richard M. Stallman <rms@gnu.org>
parents: 9931
diff changeset
216
e961f9a213a7 (forward-same-syntax): New function.
Richard M. Stallman <rms@gnu.org>
parents: 9931
diff changeset
217 (defun forward-same-syntax (&optional arg)
e961f9a213a7 (forward-same-syntax): New function.
Richard M. Stallman <rms@gnu.org>
parents: 9931
diff changeset
218 (interactive "p")
e961f9a213a7 (forward-same-syntax): New function.
Richard M. Stallman <rms@gnu.org>
parents: 9931
diff changeset
219 (while (< arg 0)
e961f9a213a7 (forward-same-syntax): New function.
Richard M. Stallman <rms@gnu.org>
parents: 9931
diff changeset
220 (skip-syntax-backward
e961f9a213a7 (forward-same-syntax): New function.
Richard M. Stallman <rms@gnu.org>
parents: 9931
diff changeset
221 (char-to-string (char-syntax (char-after (1- (point))))))
e961f9a213a7 (forward-same-syntax): New function.
Richard M. Stallman <rms@gnu.org>
parents: 9931
diff changeset
222 (setq arg (1+ arg)))
e961f9a213a7 (forward-same-syntax): New function.
Richard M. Stallman <rms@gnu.org>
parents: 9931
diff changeset
223 (while (> arg 0)
e961f9a213a7 (forward-same-syntax): New function.
Richard M. Stallman <rms@gnu.org>
parents: 9931
diff changeset
224 (skip-syntax-forward (char-to-string (char-syntax (char-after (point)))))
e961f9a213a7 (forward-same-syntax): New function.
Richard M. Stallman <rms@gnu.org>
parents: 9931
diff changeset
225 (setq arg (1- arg))))
e961f9a213a7 (forward-same-syntax): New function.
Richard M. Stallman <rms@gnu.org>
parents: 9931
diff changeset
226
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14010
diff changeset
227 ;; Aliases
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
228
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
229 (defun word-at-point () (thing-at-point 'word))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
230 (defun sentence-at-point () (thing-at-point 'sentence))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
231
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
232 (defun read-from-whole-string (str)
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
233 "Read a lisp expression from STR.
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
234 Signal an error if the entire string was not used."
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
235 (let* ((read-data (read-from-string str))
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
236 (more-left
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
237 (condition-case nil
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
238 (progn (read-from-string (substring str (cdr read-data)))
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
239 t)
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
240 (end-of-file nil))))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
241 (if more-left
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
242 (error "Can't read whole string")
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
243 (car read-data))))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
244
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
245 (defun form-at-point (&optional thing pred)
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
246 (let ((sexp (condition-case nil
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
247 (read-from-whole-string (thing-at-point (or thing 'sexp)))
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
248 (error nil))))
16629
a3345c8d1779 (thing-at-point-url-chars): Allow period.
Richard M. Stallman <rms@gnu.org>
parents: 16427
diff changeset
249 (if (or (not pred) (funcall pred sexp)) sexp)))
4934
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
250
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
251 (defun sexp-at-point () (form-at-point 'sexp))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
252 (defun symbol-at-point () (form-at-point 'sexp 'symbolp))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
253 (defun number-at-point () (form-at-point 'sexp 'numberp))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
254 (defun list-at-point () (form-at-point 'list 'listp))
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
255
a8b355b89859 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
256 ;; thingatpt.el ends here.