5

I want a complete list of things that works with thing-at-point. The documentation states:

Possibilities include `symbol`, `list`, `sexp`, `defun`,
`filename`, `url`, `email`, `word`, `sentence`, `whitespace`,
`line`, and `page`.

See the file `thingatpt.el` for documentation on how to define a
valid THING.

Because users can dynamically define new things, I'm assuming this list needs to be dynamically computed. Things are defined by adding the appropriate properties to a symbol. Is it possible to get a list of all symbols that has a given property?

4

Yes, sure - use mapatoms:

(defun find-symbols-having-properties (properties)
  (let (ret)
    (mapatoms (lambda (s)
                (when (cl-loop for prop in properties
                               thereis (get s prop))
                    (push s ret))))
    ret))

Now, to get all the "things", we need to do

(find-symbols-having-properties
 '(beginning-op end-op bounds-of-thing-at-point thing-at-point forward-op))
==> (email url line git-revision list number buffer defun filename uuid sexp)

PS. You might not necessarily find all defined things, but the code should put you on the right path...

| improve this answer | |
3

It's not enough to test for properties beginning-op, end-op, bounds-of-thing-at-point, and thing-at-point. A thing can instead be defined by property forward-op or by a forward- THING function. (And some uses of things require the existence of a forward-op or a forward- THING function.)

Library thing-cmds.el (code) defines these functions:

  • thgcmd-defined-thing-p: Return non-nil if THING (type) is defined as a thing-at-point type. THING is a symbol.

  • thgcmd-things-alist:

thgcmd-things-alist is a compiled Lisp function in thing-cmds.el.

(thgcmd-things-alist &optional REQUIRE-FWD-P)

List of most thing types currently defined.

Each is a string that names a type of text entity for which there is a either a corresponding forward- thing operation, or corresponding beginning-of- thing and end-of- thing operations. The list includes the names of the symbols that satisfy thgcmd-defined-thing-p, but with these excluded: thing, buffer, point.

Non-nil arg REQUIRE-FWD-P means exclude THINGs that do not have an associated forward-THING function.

This is the definition of thgcmd-defined-thing-p, which sounds like what you're looking for:

(defun thgcmd-defined-thing-p (thing)
  "Return non-nil if THING (type) is defined as a thing-at-point type.
THING is a symbol."
  (let ((forward-op    (or (get thing 'forward-op)
                           (intern-soft (format "forward-%s" thing))))
        (beginning-op  (get thing 'beginning-op))
        (end-op        (get thing 'end-op))
        (bounds-fn     (get thing 'bounds-of-thing-at-point))
        (thing-fn      (get thing 'thing-at-point)))
    (or (functionp forward-op)
        (and (functionp beginning-op)  (functionp end-op))
        (functionp bounds-fn)
        (functionp thing-fn))))

There is also user option thing-types:

thing-types is a variable defined in thing-cmds.el.

Its value is

("sexp" "button" "char" "char-same-line" "color" "comment" "decimal-number" "defun" "email" "filename" "hex-number" "line" "list" "list-contents" "non-nil-symbol-name" "number" "overlay" "page" "paragraph" "region-or-word" "sentence" "string" "string-contents" "symbol" "symbol-name" "unquoted-list" "url" "whitespace" "whitespace-&-newlines" "word")

Documentation:

List of thing types.

Each is a string that names a type of text entity for which there is a either a corresponding forward-thing operation, or corresponding beginning-of-thing and end-of-thing operations.

The default value includes the names of most symbols that satisfy thgcmd-defined-thing-p at the time the defcustom is evaluated. These types are excluded: thing, buffer, point.

M-@ cycles through this list, in order.

You can customize this variable.

The actual default value depends on your setup. This is the sexp that is evaluated at defcustom time to produce the default value:

(let ((types  ()))
  (mapatoms
   (lambda (tt)
     (when (thgcmd-defined-thing-p tt) (push (symbol-name tt) types))))
  (setq types  (sort types #'string-lessp))
  ;; Remove types that do not make sense.
  (dolist (typ  '("sexp" "thing" "buffer" "point"))
    (setq types (delete typ types)))
  (setq types  (cons "sexp" types))) ; Put `sexp' first.

The option is used in command cycle-select-something, and it could also be useful in other contexts:

cycle-select-something is an interactive compiled Lisp function in thing-cmds.el.

It is bound to M-@.

(cycle-select-something)

Select something at or near point. Successively select different things.

The default thing type is the first element of option thing-types.

In Transient Mark mode, you can follow this with C-M-SPC to select successive things of the same type, but to do that you must first use C-x C-x: M-@ C-x C-x C-M-SPC.

If option thgcmd-use-nearest-thing-flag and non-nil then use a thing that is near, not necessarily at, point.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.