thingatpt.el
- 9 August 2024
Emacs's thingatpt.el
provides an API for defining text objects known as 'things', and querying the buffer for them. I want to add my own 'things' but the documentation isn't that clear (they say to do one thing, but do another internally).
However, after reading most of the functions thingatpt.el
defines, things become a lot clearer. In short, there are a few requirements to define a 'thing':
-
You must define
'forward-op
to useforward-thing
. This is the only operation you have to define since all other operations can be emulated with justforward-thing
. So for the rest of the operations we will talk about, it is assumed'forward-op
is eithernil
or are more efficient implementation exists.(put 'wordv2 'forward-op (lambda (&optional count) (interactive "p^") (setq count (or count 1)) (forward-word count)))
-
You may define both
'beginning-op
and'end-op
, or just'bounds-of-thing-at-point
to usebounds-of-thing-at-point
. If'forward-op
is not defined, you must define either one to usebounds-of-thing-at-point.
(put 'bufferv2 'end-op (lambda () (goto-char (point-max)))) (put 'bufferv2 'beginning-op (lambda () (goto-char (point-min)))) (put 'bufferv2 'bounds-of-thing-at-point (lambda () (cons (point-min) (point-max))))
-
You may define
'thing-at-point
directly to usething-at-point
.(put 'filenamev2 'thing-at-point (lambda (&optional _lax _bounds) (when-let ((filename (thing-at-point 'filename))) (setq filename (expand-file-name filename)) (and (file-exists-p filename) filename))))