Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs Lisp: Standard way to verify tree structures?

In emacs lisp, various tree structures are common. custom.el provides through the :type argument to defcustom a standard way to define the expected shape of customization variables. But is there a standard way to verify the structure of some random emacs lisp value?

Lets say, I have a list of the form

LIST = (ENTRY ...)
ENTRY = (NAME . ((1 VAL1) (2 VAL2) ...))

can I somehow define that structure similiar to a customization type and then check against that structure definition?

like image 954
kdb Avatar asked Jun 01 '14 18:06

kdb


1 Answers

In file lisp/wid-edit.el there is this function:

(defun widget-type-match (widget value)
  "Non-nil if the :type value of WIDGET matches VALUE.

The value of the :type attribute should be an unconverted widget type."
  (widget-apply (widget-convert (widget-get widget :type)) :match value))

which you can adapt to your needs:

(defun my-type-match (type value)
  (widget-apply (widget-convert type) :match value))

(my-type-match 'string "foo")
==> t
(my-type-match 'string 10)
==> nil
(my-type-match '(choice (const 1) (const 2) (const t)) 10)
==> nil
(my-type-match '(choice (const 1) (const 2) (const t)) 2)
==> t
like image 124
sds Avatar answered Oct 22 '22 03:10

sds