使用 defcustom 创建可自定义值时,如何验证用户的条目?



我正在编写一个elisp文件,该文件正在演变为一个包,因此我正在将其中的一些变量转换为defcustom语句并对其进行文档记录。其中一些defcustom变量是相关的,我想验证通过Customize系统输入的值,以确保关系成立。

下面是我的一个例子:

(defcustom widget-canonical-names '("my_widget" . "widget_assembly 8911_j4")
  "Documentation"
  :type '(alist :key-type (string :tag "Widget's short name")
                :value-type (string :tag "Full widget name"))
  :risky nil
  :group 'widgets)
(defcustom widget-colors '("my_widget" . "brown")
  "Documentation"
  :type '(alist :key-type (string :tag "Widget's short name")
                :value-type (color :tag "color of the widget"))
  :risky nil
  :group 'widgets)
(defcustom widget-paths '("my_widget" . "~/widgets")
  "Documentation"
  :type '(alist :key-type (string :tag "Widget's short name")
                :value-type (directory :tag "support files for widget"))
  :risky nil
  :group 'widgets)

所以有小部件,它们有各种设置,我需要能够通过知道小部件的短名称来访问小部件的任意设置。我想做一个某种验证功能(不幸的是,在谷歌上搜索"emacs defcustom validate"没有帮助),这样,如果用户在widget-pathswidget-colors中输入了一个不在widget-canonical-names列表中的小部件名称,他们会收到"你确定吗?"警告,并被警告不要输入不匹配的名称。我可以将这样的验证函数附加到我的defcustom吗?如果是,语法是什么?

当然,理想的做法是让用户只输入一次短名称,但我无法从"Composite Types"elisp文档中找到如何做到这一点。因此,对于我的问题,一个更好的答案是告诉我如何安排一个defcustom,它可以建立类似于以下Python dict:的数据结构

customized_widgets = {
    "my_widget": { "canonical_name": "widget_assembly 8911_j4",
                   "widget_color": "brown",
                   "widget_path": "~/widgets",
                 },
    "another_widget": { "canonical_name" : "widget_obsolete 11.0",
                        "widget_color": "blue",
                        "widget_path": "~/blue_widgets",
                      },
     }

那么:我如何获得我想要的行为,根据将用于访问设置的数据对设置进行分组,或者验证功能在用户可能输入不一致的数据时警告用户?

这将定义与Python结构最接近的Emacs等价物,dict表示为lists,内部dict的固定键表示为符号。

(defcustom my-customized-widgets ()
  "My widget customization alist"
  :type '(alist
          :tag "Widgets"
          :key-type (string :tag "Short name")
          :value-type
          (set
           :format "%v"
           :entry-format "%b %v"
           (cons :format "%v"
                 (const :format "" widget-canonical-name)
                 (string :tag "CName"))
           (cons :format "%v"
                 (const :format "" widget-color)
                 (color :tag "Color"))
           (cons :format "%v"
                 (const :format "" widget-path)
                 (directory :tag " Path"))))
  :group 'widgets)

最新更新