diff options
| author | Richard M. Stallman | 1996-12-07 21:20:10 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1996-12-07 21:20:10 +0000 |
| commit | c851323f3dc2af5f903d387602f998a67c1f8f54 (patch) | |
| tree | e0a6e790e190bea01f2ab595ab538dfc6ae31588 | |
| parent | 147268711e45b597d710d4daf5da209ca6175a8a (diff) | |
| download | emacs-c851323f3dc2af5f903d387602f998a67c1f8f54.tar.gz emacs-c851323f3dc2af5f903d387602f998a67c1f8f54.zip | |
(thing-at-point-url-chars): Allow period.
(url): Move back over periods at the end.
Downcase arguments as Lisp symbols.
Fix many doc strings.
(thing-at-point-file-name-chars): Renamed from file-name-chars.
Allow a colon.
(thing-at-point-url-chars): New variable.
(url): Define new kind of "thing".
| -rw-r--r-- | lisp/thingatpt.el | 136 |
1 files changed, 80 insertions, 56 deletions
diff --git a/lisp/thingatpt.el b/lisp/thingatpt.el index c33ae58e7e7..5631d0071dd 100644 --- a/lisp/thingatpt.el +++ b/lisp/thingatpt.el | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | ;;; thingatpt.el --- Get the `thing' at point | 1 | ;;; thingatpt.el --- Get the `thing' at point |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 1991,1992,1993,1994,1995 Free Software Foundation, Inc. | 3 | ;; Copyright (C) 1991,92,93,94,95,1996 Free Software Foundation, Inc. |
| 4 | 4 | ||
| 5 | ;; Author: Mike Williams <mikew@gopher.dosli.govt.nz> | 5 | ;; Author: Mike Williams <mikew@gopher.dosli.govt.nz> |
| 6 | ;; Keywords: extensions, matching, mouse | 6 | ;; Keywords: extensions, matching, mouse |
| @@ -20,23 +20,23 @@ | |||
| 20 | 20 | ||
| 21 | ;;; Commentary: | 21 | ;;; Commentary: |
| 22 | 22 | ||
| 23 | ;; This file provides routines for getting the `thing' at the location of | 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 | 24 | ;; point, whatever that "thing" happens to be. The "thing" is defined by |
| 25 | ;; its beginning and end positions in the buffer. | 25 | ;; its beginning and end positions in the buffer. |
| 26 | ;; | 26 | ;; |
| 27 | ;; The function bounds-of-thing-at-point finds the beginning and end | 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 | 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 | 29 | ;; backwards to the beginning. By default, it uses the corresponding |
| 30 | ;; forward-`thing' operator (eg. forward-word, forward-line). | 30 | ;; forward-"thing" operator (eg. forward-word, forward-line). |
| 31 | ;; | 31 | ;; |
| 32 | ;; Special cases are allowed for using properties associated with the named | 32 | ;; Special cases are allowed for using properties associated with the named |
| 33 | ;; `thing': | 33 | ;; "thing": |
| 34 | ;; | 34 | ;; |
| 35 | ;; forward-op Function to call to skip forward over a `thing' (or | 35 | ;; forward-op Function to call to skip forward over a "thing" (or |
| 36 | ;; with a negative argument, backward). | 36 | ;; with a negative argument, backward). |
| 37 | ;; | 37 | ;; |
| 38 | ;; beginning-op Function to call to skip to the beginning of a `thing'. | 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'. | 39 | ;; end-op Function to call to skip to the end of a "thing". |
| 40 | ;; | 40 | ;; |
| 41 | ;; Reliance on existing operators means that many `things' can be accessed | 41 | ;; Reliance on existing operators means that many `things' can be accessed |
| 42 | ;; without further code: eg. | 42 | ;; without further code: eg. |
| @@ -50,58 +50,68 @@ | |||
| 50 | ;; Basic movement | 50 | ;; Basic movement |
| 51 | 51 | ||
| 52 | ;;;###autoload | 52 | ;;;###autoload |
| 53 | (defun forward-thing (THING &optional N) | 53 | (defun forward-thing (thing &optional n) |
| 54 | "Move forward to the end of the next THING." | 54 | "Move forward to the end of the next THING." |
| 55 | (let ((forward-op (or (get THING 'forward-op) | 55 | (let ((forward-op (or (get thing 'forward-op) |
| 56 | (intern-soft (format "forward-%s" THING))))) | 56 | (intern-soft (format "forward-%s" thing))))) |
| 57 | (if (fboundp forward-op) | 57 | (if (fboundp forward-op) |
| 58 | (funcall forward-op (or N 1)) | 58 | (funcall forward-op (or n 1)) |
| 59 | (error "Can't determine how to move over %ss" THING)))) | 59 | (error "Can't determine how to move over a %s" thing)))) |
| 60 | 60 | ||
| 61 | ;; General routines | 61 | ;; General routines |
| 62 | 62 | ||
| 63 | ;;;###autoload | 63 | ;;;###autoload |
| 64 | (defun bounds-of-thing-at-point (THING) | 64 | (defun bounds-of-thing-at-point (thing) |
| 65 | "Determine the start and end buffer locations for the THING at point, | 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 | 66 | THING is a symbol which specifies the kind of syntactic entity you want. |
| 67 | forward-THING operation, or corresponding beginning-of-THING and | 67 | Possibilities include `symbol', `list', `sexp', `defun', `filename', `url', |
| 68 | end-of-THING operations, eg. 'word, 'sentence, 'defun. | 68 | `word', `sentence', `whitespace', `line', `page' and others. |
| 69 | Return a cons cell '(start . end) giving the start and end positions." | 69 | |
| 70 | See the file `thingatpt.el' for documentation on how to define | ||
| 71 | a symbol as a valid THING. | ||
| 72 | |||
| 73 | The value is a cons cell (START . END) giving the start and end positions | ||
| 74 | of the textual entity that was found." | ||
| 70 | (let ((orig (point))) | 75 | (let ((orig (point))) |
| 71 | (condition-case nil | 76 | (condition-case nil |
| 72 | (save-excursion | 77 | (save-excursion |
| 73 | (let ((end (progn | 78 | (let ((end (progn |
| 74 | (funcall | 79 | (funcall |
| 75 | (or (get THING 'end-op) | 80 | (or (get thing 'end-op) |
| 76 | (function (lambda () (forward-thing THING 1))))) | 81 | (function (lambda () (forward-thing thing 1))))) |
| 77 | (point))) | 82 | (point))) |
| 78 | (beg (progn | 83 | (beg (progn |
| 79 | (funcall | 84 | (funcall |
| 80 | (or (get THING 'beginning-op) | 85 | (or (get thing 'beginning-op) |
| 81 | (function (lambda () (forward-thing THING -1))))) | 86 | (function (lambda () (forward-thing thing -1))))) |
| 82 | (point)))) | 87 | (point)))) |
| 83 | (if (and beg end (<= beg orig) (< orig end)) | 88 | (if (and beg end (<= beg orig) (< orig end)) |
| 84 | (cons beg end)))) | 89 | (cons beg end)))) |
| 85 | (error nil)))) | 90 | (error nil)))) |
| 86 | 91 | ||
| 87 | ;;;###autoload | 92 | ;;;###autoload |
| 88 | (defun thing-at-point (THING) | 93 | (defun thing-at-point (thing) |
| 89 | "Return the THING at point, where THING is an entity defined by | 94 | "Return the THING at point. |
| 90 | bounds-of-thing-at-point." | 95 | THING is a symbol which specifies the kind of syntactic entity you want. |
| 91 | (let ((bounds (bounds-of-thing-at-point THING))) | 96 | Possibilities include `symbol', `list', `sexp', `defun', `filename', `url', |
| 97 | `word', `sentence', `whitespace', `line', `page' and others. | ||
| 98 | |||
| 99 | See the file `thingatpt.el' for documentation on how to define | ||
| 100 | a symbol as a valid THING." | ||
| 101 | (let ((bounds (bounds-of-thing-at-point thing))) | ||
| 92 | (if bounds | 102 | (if bounds |
| 93 | (buffer-substring (car bounds) (cdr bounds))))) | 103 | (buffer-substring (car bounds) (cdr bounds))))) |
| 94 | 104 | ||
| 95 | ;; Go to beginning/end | 105 | ;; Go to beginning/end |
| 96 | 106 | ||
| 97 | (defun beginning-of-thing (THING) | 107 | (defun beginning-of-thing (thing) |
| 98 | (let ((bounds (bounds-of-thing-at-point THING))) | 108 | (let ((bounds (bounds-of-thing-at-point thing))) |
| 99 | (or bounds (error "No %s here" THING)) | 109 | (or bounds (error "No %s here" thing)) |
| 100 | (goto-char (car bounds)))) | 110 | (goto-char (car bounds)))) |
| 101 | 111 | ||
| 102 | (defun end-of-thing (THING) | 112 | (defun end-of-thing (thing) |
| 103 | (let ((bounds (bounds-of-thing-at-point THING))) | 113 | (let ((bounds (bounds-of-thing-at-point thing))) |
| 104 | (or bounds (error "No %s here" THING)) | 114 | (or bounds (error "No %s here" thing)) |
| 105 | (goto-char (cdr bounds)))) | 115 | (goto-char (cdr bounds)))) |
| 106 | 116 | ||
| 107 | ;; Special cases | 117 | ;; Special cases |
| @@ -136,27 +146,41 @@ bounds-of-thing-at-point." | |||
| 136 | (put 'list 'end-op (function (lambda () (up-list 1)))) | 146 | (put 'list 'end-op (function (lambda () (up-list 1)))) |
| 137 | (put 'list 'beginning-op 'backward-sexp) | 147 | (put 'list 'beginning-op 'backward-sexp) |
| 138 | 148 | ||
| 139 | ;; Filenames | 149 | ;; Filenames and URLs |
| 140 | 150 | ||
| 141 | (defvar file-name-chars "~/A-Za-z0-9---_.${}#%," | 151 | (defvar thing-at-point-file-name-chars "~/A-Za-z0-9---_.${}#%,:" |
| 142 | "Characters allowable in filenames.") | 152 | "Characters allowable in filenames.") |
| 143 | 153 | ||
| 144 | (put 'filename 'end-op | 154 | (put 'filename 'end-op |
| 145 | (function (lambda () (skip-chars-forward file-name-chars)))) | 155 | '(lambda () (skip-chars-forward thing-at-point-file-name-chars))) |
| 146 | (put 'filename 'beginning-op | 156 | (put 'filename 'beginning-op |
| 147 | (function (lambda () (skip-chars-backward file-name-chars (point-min))))) | 157 | '(lambda () (skip-chars-backward thing-at-point-file-name-chars))) |
| 158 | |||
| 159 | (defvar thing-at-point-url-chars "~/A-Za-z0-9---_$%." | ||
| 160 | "Characters allowable in a URL.") | ||
| 161 | |||
| 162 | (put 'url 'end-op | ||
| 163 | '(lambda () (skip-chars-forward thing-at-point-url-chars) | ||
| 164 | (skip-chars-backward "."))) | ||
| 165 | (put 'url 'beginning-op | ||
| 166 | '(lambda () | ||
| 167 | (skip-chars-backward thing-at-point-url-chars) | ||
| 168 | (or (= (preceding-char) ?:) | ||
| 169 | (error "No URL here")) | ||
| 170 | (forward-char -1) | ||
| 171 | (skip-chars-backward "a-zA-Z"))) | ||
| 148 | 172 | ||
| 149 | ;; Whitespace | 173 | ;; Whitespace |
| 150 | 174 | ||
| 151 | (defun forward-whitespace (ARG) | 175 | (defun forward-whitespace (arg) |
| 152 | (interactive "p") | 176 | (interactive "p") |
| 153 | (if (natnump ARG) | 177 | (if (natnump arg) |
| 154 | (re-search-forward "[ \t]+\\|\n" nil nil ARG) | 178 | (re-search-forward "[ \t]+\\|\n" nil nil arg) |
| 155 | (while (< ARG 0) | 179 | (while (< arg 0) |
| 156 | (if (re-search-backward "[ \t]+\\|\n" nil nil) | 180 | (if (re-search-backward "[ \t]+\\|\n" nil nil) |
| 157 | (or (eq (char-after (match-beginning 0)) 10) | 181 | (or (eq (char-after (match-beginning 0)) 10) |
| 158 | (skip-chars-backward " \t"))) | 182 | (skip-chars-backward " \t"))) |
| 159 | (setq ARG (1+ ARG))))) | 183 | (setq arg (1+ arg))))) |
| 160 | 184 | ||
| 161 | ;; Buffer | 185 | ;; Buffer |
| 162 | 186 | ||
| @@ -165,14 +189,14 @@ bounds-of-thing-at-point." | |||
| 165 | 189 | ||
| 166 | ;; Symbols | 190 | ;; Symbols |
| 167 | 191 | ||
| 168 | (defun forward-symbol (ARG) | 192 | (defun forward-symbol (arg) |
| 169 | (interactive "p") | 193 | (interactive "p") |
| 170 | (if (natnump ARG) | 194 | (if (natnump arg) |
| 171 | (re-search-forward "\\(\\sw\\|\\s_\\)+" nil nil ARG) | 195 | (re-search-forward "\\(\\sw\\|\\s_\\)+" nil nil arg) |
| 172 | (while (< ARG 0) | 196 | (while (< arg 0) |
| 173 | (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil nil) | 197 | (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil nil) |
| 174 | (skip-syntax-backward "w_")) | 198 | (skip-syntax-backward "w_")) |
| 175 | (setq ARG (1+ ARG))))) | 199 | (setq arg (1+ arg))))) |
| 176 | 200 | ||
| 177 | ;; Syntax blocks | 201 | ;; Syntax blocks |
| 178 | 202 | ||
| @@ -191,24 +215,24 @@ bounds-of-thing-at-point." | |||
| 191 | (defun word-at-point () (thing-at-point 'word)) | 215 | (defun word-at-point () (thing-at-point 'word)) |
| 192 | (defun sentence-at-point () (thing-at-point 'sentence)) | 216 | (defun sentence-at-point () (thing-at-point 'sentence)) |
| 193 | 217 | ||
| 194 | (defun read-from-whole-string (STR) | 218 | (defun read-from-whole-string (str) |
| 195 | "Read a lisp expression from STR, signaling an error if the entire string | 219 | "Read a lisp expression from STR. |
| 196 | was not used." | 220 | Signal an error if the entire string was not used." |
| 197 | (let* ((read-data (read-from-string STR)) | 221 | (let* ((read-data (read-from-string str)) |
| 198 | (more-left | 222 | (more-left |
| 199 | (condition-case nil | 223 | (condition-case nil |
| 200 | (progn (read-from-string (substring STR (cdr read-data))) | 224 | (progn (read-from-string (substring str (cdr read-data))) |
| 201 | t) | 225 | t) |
| 202 | (end-of-file nil)))) | 226 | (end-of-file nil)))) |
| 203 | (if more-left | 227 | (if more-left |
| 204 | (error "Can't read whole string") | 228 | (error "Can't read whole string") |
| 205 | (car read-data)))) | 229 | (car read-data)))) |
| 206 | 230 | ||
| 207 | (defun form-at-point (&optional THING PRED) | 231 | (defun form-at-point (&optional thing pred) |
| 208 | (let ((sexp (condition-case nil | 232 | (let ((sexp (condition-case nil |
| 209 | (read-from-whole-string (thing-at-point (or THING 'sexp))) | 233 | (read-from-whole-string (thing-at-point (or thing 'sexp))) |
| 210 | (error nil)))) | 234 | (error nil)))) |
| 211 | (if (or (not PRED) (funcall PRED sexp)) sexp))) | 235 | (if (or (not pred) (funcall pred sexp)) sexp))) |
| 212 | 236 | ||
| 213 | (defun sexp-at-point () (form-at-point 'sexp)) | 237 | (defun sexp-at-point () (form-at-point 'sexp)) |
| 214 | (defun symbol-at-point () (form-at-point 'sexp 'symbolp)) | 238 | (defun symbol-at-point () (form-at-point 'sexp 'symbolp)) |