This page describes the standard GNU Emacs library
thingatpt.el and extensions thereof. The library can determine and retrieve different kinds of thing at point. The function
‘thing-at-point’ retrieves the thing at point.
To use this library in your own EmacsLisp code, do this:
The possibilities of a “thing” include symbol, list, S-expression (sexp), function definition (defun), filename, URL, word, sentence, whitespace, line, page and others. For any standard thing that has a
‘forward-thing’ function, just pass the symbol of thing to
‘thing-at-point’. If found, the thing at point is returned as a string. Here is an example for getting the sentence at the current location:
There are other useful functions provided by the library, including:
‘bounds-of-thing-at-point’returns the beginning and end positions of a thing.
‘end-of-thing’move point to the beginning or end of a thing.
The thingatpt library is quintessential Emacs, powerful but minimal. View the source code with
‘M-x find-library RET thingatpt RET’ to learn more its features and how to extend it to things of your own.
Rather than passing a symbol for a type of thing, several
‘-at-point’ functions are defined for specific types of things. For example,
These functions will usually return the appropriate object type for the particular type of thing. For example, an object of type symbol is returned for symbols and of type integer for numbers.
(sentence-at-point) ;; returns string (thing-at-point 'symbol) ;; returns string (symbol-at-point) ;; returns symbol
Specific thing-at-point functions are available for most major types of things and are defined in
thingatpt.el. Any other things you want the library to recognize you have to extend the library by defining functions that can recognize your thing.
The library behind
‘thing-at-point’ is generic and extensible to additional types of things. It relies on function
‘bounds-of-thing-at-point’, which starts from point and uses function
‘forward-thing’ to move forward, then backward to retrieve the bounds of the thing. If
‘forward-thing’ works for a particular type of thing, then so will
‘forward-thing’ takes as argument a symbol naming a type of thing. For example, argument
‘forward-thing’ to move forward one sentence. That is, it does so if a function named
‘forward-sentence’ is defined, or if the symbol
‘sentence’ has a
‘forward-op’ property on its property list. Because function
‘forward-sentence’ is defined in Emacs,
‘forward-thing’ can operate with sentences.
‘forward-thing’ work with a particular kind of thing, say, a new type
‘foobar’ that you define, you can define a function
‘forward-foobar’ that moves forward one or more
‘foobar’s. (With a negative argument,
‘forward-foobar’ should move backward.)
Libraries are available that extend library
thingatpt.el in various ways.
‘occur’, but (a) with things of any type, not just lines and (b) with dynamic search patterns – change on the fly.
The following add new kinds of things at point. They provide examples of how to use