| 1 | ;;; thingatpt.el --- get the `thing' at point |
| 2 | |
| 3 | ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000, |
| 4 | ;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
| 5 | ;; Free Software Foundation, Inc. |
| 6 | |
| 7 | ;; Author: Mike Williams <mikew@gopher.dosli.govt.nz> |
| 8 | ;; Maintainer: FSF |
| 9 | ;; Keywords: extensions, matching, mouse |
| 10 | ;; Created: Thu Mar 28 13:48:23 1991 |
| 11 | |
| 12 | ;; This file is part of GNU Emacs. |
| 13 | |
| 14 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
| 15 | ;; it under the terms of the GNU General Public License as published by |
| 16 | ;; the Free Software Foundation, either version 3 of the License, or |
| 17 | ;; (at your option) any later version. |
| 18 | |
| 19 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 20 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | ;; GNU General Public License for more details. |
| 23 | |
| 24 | ;; You should have received a copy of the GNU General Public License |
| 25 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 26 | |
| 27 | ;;; Commentary: |
| 28 | |
| 29 | ;; This file provides routines for getting the "thing" at the location of |
| 30 | ;; point, whatever that "thing" happens to be. The "thing" is defined by |
| 31 | ;; its beginning and end positions in the buffer. |
| 32 | ;; |
| 33 | ;; The function bounds-of-thing-at-point finds the beginning and end |
| 34 | ;; positions by moving first forward to the end of the "thing", and then |
| 35 | ;; backwards to the beginning. By default, it uses the corresponding |
| 36 | ;; forward-"thing" operator (eg. forward-word, forward-line). |
| 37 | ;; |
| 38 | ;; Special cases are allowed for using properties associated with the named |
| 39 | ;; "thing": |
| 40 | ;; |
| 41 | ;; forward-op Function to call to skip forward over a "thing" (or |
| 42 | ;; with a negative argument, backward). |
| 43 | ;; |
| 44 | ;; beginning-op Function to call to skip to the beginning of a "thing". |
| 45 | ;; end-op Function to call to skip to the end of a "thing". |
| 46 | ;; |
| 47 | ;; Reliance on existing operators means that many `things' can be accessed |
| 48 | ;; without further code: eg. |
| 49 | ;; (thing-at-point 'line) |
| 50 | ;; (thing-at-point 'page) |
| 51 | |
| 52 | ;;; Code: |
| 53 | |
| 54 | (provide 'thingatpt) |
| 55 | |
| 56 | ;; Basic movement |
| 57 | |
| 58 | ;;;###autoload |
| 59 | (defun forward-thing (thing &optional n) |
| 60 | "Move forward to the end of the Nth next THING." |
| 61 | (let ((forward-op (or (get thing 'forward-op) |
| 62 | (intern-soft (format "forward-%s" thing))))) |
| 63 | (if (functionp forward-op) |
| 64 | (funcall forward-op (or n 1)) |
| 65 | (error "Can't determine how to move over a %s" thing)))) |
| 66 | |
| 67 | ;; General routines |
| 68 | |
| 69 | ;;;###autoload |
| 70 | (defun bounds-of-thing-at-point (thing) |
| 71 | "Determine the start and end buffer locations for the THING at point. |
| 72 | THING is a symbol which specifies the kind of syntactic entity you want. |
| 73 | Possibilities include `symbol', `list', `sexp', `defun', `filename', `url', |
| 74 | `email', `word', `sentence', `whitespace', `line', `page' and others. |
| 75 | |
| 76 | See the file `thingatpt.el' for documentation on how to define |
| 77 | a symbol as a valid THING. |
| 78 | |
| 79 | The value is a cons cell (START . END) giving the start and end positions |
| 80 | of the textual entity that was found." |
| 81 | (if (get thing 'bounds-of-thing-at-point) |
| 82 | (funcall (get thing 'bounds-of-thing-at-point)) |
| 83 | (let ((orig (point))) |
| 84 | (condition-case nil |
| 85 | (save-excursion |
| 86 | ;; Try moving forward, then back. |
| 87 | (funcall ;; First move to end. |
| 88 | (or (get thing 'end-op) |
| 89 | (lambda () (forward-thing thing 1)))) |
| 90 | (funcall ;; Then move to beg. |
| 91 | (or (get thing 'beginning-op) |
| 92 | (lambda () (forward-thing thing -1)))) |
| 93 | (let ((beg (point))) |
| 94 | (if (not (and beg (> beg orig))) |
| 95 | ;; If that brings us all the way back to ORIG, |
| 96 | ;; it worked. But END may not be the real end. |
| 97 | ;; So find the real end that corresponds to BEG. |
| 98 | (let ((real-end |
| 99 | (progn |
| 100 | (funcall |
| 101 | (or (get thing 'end-op) |
| 102 | (lambda () (forward-thing thing 1)))) |
| 103 | (point)))) |
| 104 | (if (and beg real-end (<= beg orig) (<= orig real-end)) |
| 105 | (cons beg real-end))) |
| 106 | (goto-char orig) |
| 107 | ;; Try a second time, moving backward first and then forward, |
| 108 | ;; so that we can find a thing that ends at ORIG. |
| 109 | (funcall ;; First, move to beg. |
| 110 | (or (get thing 'beginning-op) |
| 111 | (lambda () (forward-thing thing -1)))) |
| 112 | (funcall ;; Then move to end. |
| 113 | (or (get thing 'end-op) |
| 114 | (lambda () (forward-thing thing 1)))) |
| 115 | (let ((end (point)) |
| 116 | (real-beg |
| 117 | (progn |
| 118 | (funcall |
| 119 | (or (get thing 'beginning-op) |
| 120 | (lambda () (forward-thing thing -1)))) |
| 121 | (point)))) |
| 122 | (if (and real-beg end (<= real-beg orig) (<= orig end)) |
| 123 | (cons real-beg end)))))) |
| 124 | (error nil))))) |
| 125 | |
| 126 | ;;;###autoload |
| 127 | (defun thing-at-point (thing) |
| 128 | "Return the THING at point. |
| 129 | THING is a symbol which specifies the kind of syntactic entity you want. |
| 130 | Possibilities include `symbol', `list', `sexp', `defun', `filename', `url', |
| 131 | `email', `word', `sentence', `whitespace', `line', `page' and others. |
| 132 | |
| 133 | See the file `thingatpt.el' for documentation on how to define |
| 134 | a symbol as a valid THING." |
| 135 | (if (get thing 'thing-at-point) |
| 136 | (funcall (get thing 'thing-at-point)) |
| 137 | (let ((bounds (bounds-of-thing-at-point thing))) |
| 138 | (if bounds |
| 139 | (buffer-substring (car bounds) (cdr bounds)))))) |
| 140 | |
| 141 | ;; Go to beginning/end |
| 142 | |
| 143 | (defun beginning-of-thing (thing) |
| 144 | (let ((bounds (bounds-of-thing-at-point thing))) |
| 145 | (or bounds (error "No %s here" thing)) |
| 146 | (goto-char (car bounds)))) |
| 147 | |
| 148 | (defun end-of-thing (thing) |
| 149 | (let ((bounds (bounds-of-thing-at-point thing))) |
| 150 | (or bounds (error "No %s here" thing)) |
| 151 | (goto-char (cdr bounds)))) |
| 152 | |
| 153 | ;; Special cases |
| 154 | |
| 155 | ;; Lines |
| 156 | |
| 157 | ;; bolp will be false when you click on the last line in the buffer |
| 158 | ;; and it has no final newline. |
| 159 | |
| 160 | (put 'line 'beginning-op |
| 161 | (lambda () (if (bolp) (forward-line -1) (beginning-of-line)))) |
| 162 | |
| 163 | ;; Sexps |
| 164 | |
| 165 | (defun in-string-p () |
| 166 | (let ((orig (point))) |
| 167 | (save-excursion |
| 168 | (beginning-of-defun) |
| 169 | (nth 3 (parse-partial-sexp (point) orig))))) |
| 170 | |
| 171 | (defun end-of-sexp () |
| 172 | (let ((char-syntax (char-syntax (char-after)))) |
| 173 | (if (or (eq char-syntax ?\)) |
| 174 | (and (eq char-syntax ?\") (in-string-p))) |
| 175 | (forward-char 1) |
| 176 | (forward-sexp 1)))) |
| 177 | |
| 178 | (put 'sexp 'end-op 'end-of-sexp) |
| 179 | |
| 180 | (defun beginning-of-sexp () |
| 181 | (let ((char-syntax (char-syntax (char-before)))) |
| 182 | (if (or (eq char-syntax ?\() |
| 183 | (and (eq char-syntax ?\") (in-string-p))) |
| 184 | (forward-char -1) |
| 185 | (forward-sexp -1)))) |
| 186 | |
| 187 | (put 'sexp 'beginning-op 'beginning-of-sexp) |
| 188 | |
| 189 | ;; Lists |
| 190 | |
| 191 | (put 'list 'bounds-of-thing-at-point 'thing-at-point-bounds-of-list-at-point) |
| 192 | |
| 193 | (defun thing-at-point-bounds-of-list-at-point () |
| 194 | (save-excursion |
| 195 | (let ((opoint (point)) |
| 196 | (beg (condition-case nil |
| 197 | (progn (up-list -1) |
| 198 | (point)) |
| 199 | (error nil)))) |
| 200 | (condition-case nil |
| 201 | (if beg |
| 202 | (progn (forward-sexp) |
| 203 | (cons beg (point))) |
| 204 | ;; Are we are at the beginning of a top-level sexp? |
| 205 | (forward-sexp) |
| 206 | (let ((end (point))) |
| 207 | (backward-sexp) |
| 208 | (if (>= opoint (point)) |
| 209 | (cons opoint end)))) |
| 210 | (error nil))))) |
| 211 | |
| 212 | ;; Filenames and URLs www.com/foo%32bar |
| 213 | |
| 214 | (defvar thing-at-point-file-name-chars "-~/[:alnum:]_.${}#%,:" |
| 215 | "Characters allowable in filenames.") |
| 216 | |
| 217 | (put 'filename 'end-op |
| 218 | (lambda () |
| 219 | (re-search-forward (concat "\\=[" thing-at-point-file-name-chars "]*") |
| 220 | nil t))) |
| 221 | (put 'filename 'beginning-op |
| 222 | (lambda () |
| 223 | (if (re-search-backward (concat "[^" thing-at-point-file-name-chars "]") |
| 224 | nil t) |
| 225 | (forward-char) |
| 226 | (goto-char (point-min))))) |
| 227 | |
| 228 | (defvar thing-at-point-url-path-regexp |
| 229 | "[^]\t\n \"'<>[^`{}]*[^]\t\n \"'<>[^`{}.,;]+" |
| 230 | "A regular expression probably matching the host and filename or e-mail part of a URL.") |
| 231 | |
| 232 | (defvar thing-at-point-short-url-regexp |
| 233 | (concat "[-A-Za-z0-9.]+" thing-at-point-url-path-regexp) |
| 234 | "A regular expression probably matching a URL without an access scheme. |
| 235 | Hostname matching is stricter in this case than for |
| 236 | ``thing-at-point-url-regexp''.") |
| 237 | |
| 238 | (defvar thing-at-point-uri-schemes |
| 239 | ;; Officials from http://www.iana.org/assignments/uri-schemes.html |
| 240 | '("ftp://" "http://" "gopher://" "mailto:" "news:" "nntp:" |
| 241 | "telnet://" "wais://" "file:/" "prospero:" "z39.50s:" "z39.50r:" |
| 242 | "cid:" "mid:" "vemmi:" "service:" "imap:" "nfs:" "acap:" "rtsp:" |
| 243 | "tip:" "pop:" "data:" "dav:" "opaquelocktoken:" "sip:" "tel:" "fax:" |
| 244 | "modem:" "ldap:" "https://" "soap.beep:" "soap.beeps:" "urn:" "go:" |
| 245 | "afs:" "tn3270:" "mailserver:" |
| 246 | "crid:" "dict:" "dns:" "dtn:" "h323:" "im:" "info:" "ipp:" |
| 247 | "iris.beep:" "mtqp:" "mupdate:" "pres:" "sips:" "snmp:" "tag:" |
| 248 | "tftp:" "xmlrpc.beep:" "xmlrpc.beeps:" "xmpp:" |
| 249 | ;; Compatibility |
| 250 | "snews:" "irc:" "mms://" "mmsh://") |
| 251 | "Uniform Resource Identifier (URI) Schemes.") |
| 252 | |
| 253 | (defvar thing-at-point-url-regexp |
| 254 | (concat "\\<\\(" (mapconcat 'identity thing-at-point-uri-schemes "\\|") "\\)" |
| 255 | thing-at-point-url-path-regexp) |
| 256 | "A regular expression probably matching a complete URL.") |
| 257 | |
| 258 | (defvar thing-at-point-markedup-url-regexp |
| 259 | "<URL:[^>]+>" |
| 260 | "A regular expression matching a URL marked up per RFC1738. |
| 261 | This may contain whitespace (including newlines) .") |
| 262 | |
| 263 | (put 'url 'bounds-of-thing-at-point 'thing-at-point-bounds-of-url-at-point) |
| 264 | (defun thing-at-point-bounds-of-url-at-point () |
| 265 | (let ((strip (thing-at-point-looking-at |
| 266 | thing-at-point-markedup-url-regexp))) ;; (url "") short |
| 267 | (if (or strip |
| 268 | (thing-at-point-looking-at thing-at-point-url-regexp) |
| 269 | ;; Access scheme omitted? |
| 270 | ;; (setq short (thing-at-point-looking-at |
| 271 | ;; thing-at-point-short-url-regexp)) |
| 272 | ) |
| 273 | (let ((beginning (match-beginning 0)) |
| 274 | (end (match-end 0))) |
| 275 | (when strip |
| 276 | (setq beginning (+ beginning 5)) |
| 277 | (setq end (- end 1))) |
| 278 | (cons beginning end))))) |
| 279 | |
| 280 | (put 'url 'thing-at-point 'thing-at-point-url-at-point) |
| 281 | (defun thing-at-point-url-at-point () |
| 282 | "Return the URL around or before point. |
| 283 | |
| 284 | Search backwards for the start of a URL ending at or after point. If |
| 285 | no URL found, return nil. The access scheme will be prepended if |
| 286 | absent: \"mailto:\" if the string contains \"@\", \"ftp://\" if it |
| 287 | starts with \"ftp\" and not \"ftp:/\", or \"http://\" by default." |
| 288 | |
| 289 | (let ((url "") short strip) |
| 290 | (if (or (setq strip (thing-at-point-looking-at |
| 291 | thing-at-point-markedup-url-regexp)) |
| 292 | (thing-at-point-looking-at thing-at-point-url-regexp) |
| 293 | ;; Access scheme omitted? |
| 294 | (setq short (thing-at-point-looking-at |
| 295 | thing-at-point-short-url-regexp))) |
| 296 | (progn |
| 297 | (setq url (buffer-substring-no-properties (match-beginning 0) |
| 298 | (match-end 0))) |
| 299 | (and strip (setq url (substring url 5 -1))) ; Drop "<URL:" & ">" |
| 300 | ;; strip whitespace |
| 301 | (while (string-match "[ \t\n\r]+" url) |
| 302 | (setq url (replace-match "" t t url))) |
| 303 | (and short (setq url (concat (cond ((string-match "^[a-zA-Z]+:" url) |
| 304 | ;; already has a URL scheme. |
| 305 | "") |
| 306 | ((string-match "@" url) |
| 307 | "mailto:") |
| 308 | ;; e.g. ftp.swiss... or ftp-swiss... |
| 309 | ((string-match "^ftp" url) |
| 310 | "ftp://") |
| 311 | (t "http://")) |
| 312 | url))) |
| 313 | (if (string-equal "" url) |
| 314 | nil |
| 315 | url))))) |
| 316 | |
| 317 | ;; The normal thingatpt mechanism doesn't work for complex regexps. |
| 318 | ;; This should work for almost any regexp wherever we are in the |
| 319 | ;; match. To do a perfect job for any arbitrary regexp would mean |
| 320 | ;; testing every position before point. Regexp searches won't find |
| 321 | ;; matches that straddle the start position so we search forwards once |
| 322 | ;; and then back repeatedly and then back up a char at a time. |
| 323 | |
| 324 | (defun thing-at-point-looking-at (regexp) |
| 325 | "Return non-nil if point is in or just after a match for REGEXP. |
| 326 | Set the match data from the earliest such match ending at or after |
| 327 | point." |
| 328 | (save-excursion |
| 329 | (let ((old-point (point)) match) |
| 330 | (and (looking-at regexp) |
| 331 | (>= (match-end 0) old-point) |
| 332 | (setq match (point))) |
| 333 | ;; Search back repeatedly from end of next match. |
| 334 | ;; This may fail if next match ends before this match does. |
| 335 | (re-search-forward regexp nil 'limit) |
| 336 | (while (and (re-search-backward regexp nil t) |
| 337 | (or (> (match-beginning 0) old-point) |
| 338 | (and (looking-at regexp) ; Extend match-end past search start |
| 339 | (>= (match-end 0) old-point) |
| 340 | (setq match (point)))))) |
| 341 | (if (not match) nil |
| 342 | (goto-char match) |
| 343 | ;; Back up a char at a time in case search skipped |
| 344 | ;; intermediate match straddling search start pos. |
| 345 | (while (and (not (bobp)) |
| 346 | (progn (backward-char 1) (looking-at regexp)) |
| 347 | (>= (match-end 0) old-point) |
| 348 | (setq match (point)))) |
| 349 | (goto-char match) |
| 350 | (looking-at regexp))))) |
| 351 | |
| 352 | (put 'url 'end-op |
| 353 | (lambda () |
| 354 | (let ((bounds (thing-at-point-bounds-of-url-at-point))) |
| 355 | (if bounds |
| 356 | (goto-char (cdr bounds)) |
| 357 | (error "No URL here"))))) |
| 358 | (put 'url 'beginning-op |
| 359 | (lambda () |
| 360 | (let ((bounds (thing-at-point-bounds-of-url-at-point))) |
| 361 | (if bounds |
| 362 | (goto-char (car bounds)) |
| 363 | (error "No URL here"))))) |
| 364 | |
| 365 | ;; Email addresses |
| 366 | (defvar thing-at-point-email-regexp |
| 367 | "<?[-+_.~a-zA-Z][-+_.~:a-zA-Z0-9]*@[-.a-zA-Z0-9]+>?" |
| 368 | "A regular expression probably matching an email address. |
| 369 | This does not match the real name portion, only the address, optionally |
| 370 | with angle brackets.") |
| 371 | |
| 372 | ;; Haven't set 'forward-op on 'email nor defined 'forward-email' because |
| 373 | ;; not sure they're actually needed, and URL seems to skip them too. |
| 374 | ;; Note that (end-of-thing 'email) and (beginning-of-thing 'email) |
| 375 | ;; work automagically, though. |
| 376 | |
| 377 | (put 'email 'bounds-of-thing-at-point |
| 378 | (lambda () |
| 379 | (let ((thing (thing-at-point-looking-at thing-at-point-email-regexp))) |
| 380 | (if thing |
| 381 | (let ((beginning (match-beginning 0)) |
| 382 | (end (match-end 0))) |
| 383 | (cons beginning end)))))) |
| 384 | |
| 385 | (put 'email 'thing-at-point |
| 386 | (lambda () |
| 387 | (let ((boundary-pair (bounds-of-thing-at-point 'email))) |
| 388 | (if boundary-pair |
| 389 | (buffer-substring-no-properties |
| 390 | (car boundary-pair) (cdr boundary-pair)))))) |
| 391 | |
| 392 | ;; Whitespace |
| 393 | |
| 394 | (defun forward-whitespace (arg) |
| 395 | (interactive "p") |
| 396 | (if (natnump arg) |
| 397 | (re-search-forward "[ \t]+\\|\n" nil 'move arg) |
| 398 | (while (< arg 0) |
| 399 | (if (re-search-backward "[ \t]+\\|\n" nil 'move) |
| 400 | (or (eq (char-after (match-beginning 0)) 10) |
| 401 | (skip-chars-backward " \t"))) |
| 402 | (setq arg (1+ arg))))) |
| 403 | |
| 404 | ;; Buffer |
| 405 | |
| 406 | (put 'buffer 'end-op (lambda () (goto-char (point-max)))) |
| 407 | (put 'buffer 'beginning-op (lambda () (goto-char (point-min)))) |
| 408 | |
| 409 | ;; Symbols |
| 410 | |
| 411 | (defun forward-symbol (arg) |
| 412 | (interactive "p") |
| 413 | (if (natnump arg) |
| 414 | (re-search-forward "\\(\\sw\\|\\s_\\)+" nil 'move arg) |
| 415 | (while (< arg 0) |
| 416 | (if (re-search-backward "\\(\\sw\\|\\s_\\)+" nil 'move) |
| 417 | (skip-syntax-backward "w_")) |
| 418 | (setq arg (1+ arg))))) |
| 419 | |
| 420 | ;; Syntax blocks |
| 421 | |
| 422 | (defun forward-same-syntax (&optional arg) |
| 423 | (interactive "p") |
| 424 | (while (< arg 0) |
| 425 | (skip-syntax-backward |
| 426 | (char-to-string (char-syntax (char-before)))) |
| 427 | (setq arg (1+ arg))) |
| 428 | (while (> arg 0) |
| 429 | (skip-syntax-forward (char-to-string (char-syntax (char-after)))) |
| 430 | (setq arg (1- arg)))) |
| 431 | |
| 432 | ;; Aliases |
| 433 | |
| 434 | (defun word-at-point () (thing-at-point 'word)) |
| 435 | (defun sentence-at-point () (thing-at-point 'sentence)) |
| 436 | |
| 437 | (defun read-from-whole-string (str) |
| 438 | "Read a Lisp expression from STR. |
| 439 | Signal an error if the entire string was not used." |
| 440 | (let* ((read-data (read-from-string str)) |
| 441 | (more-left |
| 442 | (condition-case nil |
| 443 | ;; The call to `ignore' suppresses a compiler warning. |
| 444 | (progn (ignore (read-from-string (substring str (cdr read-data)))) |
| 445 | t) |
| 446 | (end-of-file nil)))) |
| 447 | (if more-left |
| 448 | (error "Can't read whole string") |
| 449 | (car read-data)))) |
| 450 | |
| 451 | (defun form-at-point (&optional thing pred) |
| 452 | (let ((sexp (condition-case nil |
| 453 | (read-from-whole-string (thing-at-point (or thing 'sexp))) |
| 454 | (error nil)))) |
| 455 | (if (or (not pred) (funcall pred sexp)) sexp))) |
| 456 | |
| 457 | ;;;###autoload |
| 458 | (defun sexp-at-point () |
| 459 | "Return the sexp at point, or nil if none is found." |
| 460 | (form-at-point 'sexp)) |
| 461 | ;;;###autoload |
| 462 | (defun symbol-at-point () |
| 463 | "Return the symbol at point, or nil if none is found." |
| 464 | (let ((thing (thing-at-point 'symbol))) |
| 465 | (if thing (intern thing)))) |
| 466 | ;;;###autoload |
| 467 | (defun number-at-point () |
| 468 | "Return the number at point, or nil if none is found." |
| 469 | (form-at-point 'sexp 'numberp)) |
| 470 | ;;;###autoload |
| 471 | (defun list-at-point () |
| 472 | "Return the Lisp list at point, or nil if none is found." |
| 473 | (form-at-point 'list 'listp)) |
| 474 | |
| 475 | ;; arch-tag: bb65a163-dae2-4055-aedc-fe11f497f698 |
| 476 | ;;; thingatpt.el ends here |