Mercurial > emacs
annotate lisp/thingatpt.el @ 5956:586a37656a31
(interpreter-mode-alist): New variable.
(set-auto-mode): Use that for chosing a mode.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Wed, 16 Feb 1994 07:06:10 +0000 |
parents | 59280129eaac |
children | c0cc87942423 |
rev | line source |
---|---|
4934 | 1 ;;; thingatpt.el --- Get the `thing' at point |
2 | |
3 ;; Copyright (C) 1991,1992,1993 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Mike Williams <mikew@gopher.dosli.govt.nz> | |
5140 | 6 ;; Keywords: extensions, matching, mouse |
4934 | 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 | |
5751
59280129eaac
(thing@pt-version): Removed variable.
Richard M. Stallman <rms@gnu.org>
parents:
5140
diff
changeset
|
21 ;;; Commentary: =========================================================== |
4934 | 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 ;; it's 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 | |
5751
59280129eaac
(thing@pt-version): Removed variable.
Richard M. Stallman <rms@gnu.org>
parents:
5140
diff
changeset
|
46 ;;; Code: ================================================================= |
4934 | 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 %ss" 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 where THING is an entity for which there is a either a corresponding | |
67 forward-THING operation, or corresponding beginning-of-THING and | |
68 end-of-THING operations, eg. 'word, 'sentence, 'defun. | |
69 Return a cons cell '(start . end) giving the start and end positions." | |
70 (let ((orig (point))) | |
71 (condition-case nil | |
72 (save-excursion | |
73 (let ((end (progn | |
74 (funcall | |
75 (or (get THING 'end-op) | |
76 (function (lambda () (forward-thing THING 1))))) | |
77 (point))) | |
78 (beg (progn | |
79 (funcall | |
80 (or (get THING 'beginning-op) | |
81 (function (lambda () (forward-thing THING -1))))) | |
82 (point)))) | |
83 (if (and beg end (<= beg orig) (< orig end)) | |
84 (cons beg end)))) | |
85 (error nil)))) | |
86 | |
87 ;;;###autoload | |
88 (defun thing-at-point (THING) | |
89 "Return the THING at point, where THING is an entity defined by | |
90 bounds-of-thing-at-point." | |
91 (let ((bounds (bounds-of-thing-at-point THING))) | |
92 (if bounds | |
93 (buffer-substring (car bounds) (cdr bounds))))) | |
94 | |
95 ;;=== Go to beginning/end ================================================= | |
96 | |
97 (defun beginning-of-thing (THING) | |
98 (let ((bounds (bounds-of-thing-at-point THING))) | |
99 (or bounds (error "No %s here" THING)) | |
100 (goto-char (car bounds)))) | |
101 | |
102 (defun end-of-thing (THING) | |
103 (let ((bounds (bounds-of-thing-at-point THING))) | |
104 (or bounds (error "No %s here" THING)) | |
105 (goto-char (cdr bounds)))) | |
106 | |
107 ;;=== Special cases ======================================================= | |
108 | |
109 ;;--- Sexps --- | |
110 | |
111 (defun in-string-p () | |
112 (let ((orig (point))) | |
113 (save-excursion | |
114 (beginning-of-defun) | |
115 (nth 3 (parse-partial-sexp (point) orig))))) | |
116 | |
117 (defun end-of-sexp () | |
118 (let ((char-syntax (char-syntax (char-after (point))))) | |
119 (if (or (eq char-syntax ?\)) | |
120 (and (eq char-syntax ?\") (in-string-p))) | |
121 (forward-char 1) | |
122 (forward-sexp 1)))) | |
123 | |
124 (put 'sexp 'end-op 'end-of-sexp) | |
125 | |
126 ;;--- Lists --- | |
127 | |
128 (put 'list 'end-op (function (lambda () (up-list 1)))) | |
129 (put 'list 'beginning-op 'backward-sexp) | |
130 | |
131 ;;--- Filenames --- | |
132 | |
133 (defvar file-name-chars "~/A-Za-z0-9---_.${}#%," | |
134 "Characters allowable in filenames.") | |
135 | |
136 (put 'filename 'end-op | |
137 (function (lambda () (skip-chars-forward file-name-chars)))) | |
138 (put 'filename 'beginning-op | |
139 (function (lambda () (skip-chars-backward file-name-chars (point-min))))) | |
140 | |
141 ;;--- Whitespace --- | |
142 | |
143 (defun forward-whitespace (ARG) | |
144 (interactive "p") | |
145 (if (natnump ARG) | |
146 (re-search-forward "[ \t]+\\|\n" nil nil ARG) | |
147 (while (< ARG 0) | |
148 (if (re-search-backward "[ \t]+\\|\n" nil nil) | |
149 (or (eq (char-after (match-beginning 0)) 10) | |
150 (skip-chars-backward " \t"))) | |
151 (setq ARG (1+ ARG))))) | |
152 | |
153 ;;--- Buffer --- | |
154 | |
155 (put 'buffer 'end-op 'end-of-buffer) | |
156 (put 'buffer 'beginning-op 'beginning-of-buffer) | |
157 | |
158 ;;--- Symbols --- | |
159 | |
160 (defun forward-symbol (ARG) | |
161 (interactive "p") | |
162 (if (natnump ARG) | |
163 (re-search-forward "\\(\\sw\\|\\s_\\)+" nil nil ARG) | |
164 (while (< ARG 0) | |
165 (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil nil) | |
166 (skip-syntax-backward "w_")) | |
167 (setq ARG (1+ ARG))))) | |
168 | |
169 ;;=== Aliases ============================================================= | |
170 | |
171 (defun word-at-point () (thing-at-point 'word)) | |
172 (defun sentence-at-point () (thing-at-point 'sentence)) | |
173 | |
174 (defun read-from-whole-string (STR) | |
175 "Read a lisp expression from STR, signalling an error if the entire string | |
176 was not used." | |
177 (let* ((read-data (read-from-string STR)) | |
178 (more-left | |
179 (condition-case nil | |
180 (progn (read-from-string (substring STR (cdr read-data))) | |
181 t) | |
182 (end-of-file nil)))) | |
183 (if more-left | |
184 (error "Can't read whole string") | |
185 (car read-data)))) | |
186 | |
187 (defun form-at-point (&optional THING PRED) | |
188 (let ((sexp (condition-case nil | |
189 (read-from-whole-string (thing-at-point (or THING 'sexp))) | |
190 (error nil)))) | |
191 (if (or (not PRED) (funcall PRED sexp)) sexp))) | |
192 | |
193 (defun sexp-at-point () (form-at-point 'sexp)) | |
194 (defun symbol-at-point () (form-at-point 'sexp 'symbolp)) | |
195 (defun number-at-point () (form-at-point 'sexp 'numberp)) | |
196 (defun list-at-point () (form-at-point 'list 'listp)) | |
197 | |
198 ;; thingatpt.el ends here. |