Emacs Lisp:验证树结构的标准方法?

时间:2014-06-01 18:33:08

标签: data-structures types elisp

在emacs lisp中,各种树结构很常见。 custom.el通过:type参数提供defcustom标准方法来定义自定义变量的预期形状。但有没有一种标准的方法来验证一些随机emacs lisp值的结构?

让我们说,我有一个表格列表

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

我可以以某种方式定义与自定义类型类似的结构,然后检查该结构定义吗?

1 个答案:

答案 0 :(得分:5)

在文件lisp/wid-edit.el中有以下功能:

(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))

您可以根据自己的需要进行调整:

(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