简体   繁体   English

Emacs lisp:defcustom的“Fall through”类型。

[英]Emacs lisp: “Fall through” type for defcustom.

TL;DR TL; DR

Is there some standard fallback handling in the customize system, for handling partially invalid composite customization variables, eg an alist where one entry is not a cons? customize系统中是否存在一些标准的回退处理,用于处理部分无效的复合自定义变量,例如,一个条目不是缺点的alist?

Long Version 长版

The customize-mechanism of emacs is quite powerful by using composite :type arguments to defcustom provides a nifty unified interface for customizing variables. emacs的自定义机制非常强大,使用复合:type defcustom :type参数为自定义变量提供了一个漂亮的统一接口。

However, when for whatever reason a single entry of the variable is wrong the whole system breaks down and it will just show the bare s-expression. 但是,无论出于何种原因,变量的单个条目都是错误的,整个系统都会崩溃,它只会显示裸s表达式。 There is then no help to fix this expect for deleting the customizations hoping that the default value matches the type description. 然后没有任何帮助来修复这种期望删除自定义,希望默认值与类型描述匹配。

At least that is what I have experienced so far. 至少这是我迄今为止所经历的。

If I have a customization variable that is a complicated data structure, is there some mechanism that allows showing only the corrupted part of the variable as a bare s-expression? 如果我有一个复杂数据结构的自定义变量,是否有一些机制允许仅将变量的损坏部分显示为裸s表达式?

Think eg about 想想,例如

(defcustom x 
  '((org-mode . "a\\|b")
    (text-mode . "b\\|c"))
  "Some variable"
  :group 'x
  :type '(repeat
          (cons :tag "Entry"
                (function :tag "Mode" :value text-mode)
                 (regexp))))

Normally Mx customize-variable x will no display a nice input mask. 通常Mx customize-variable x不会显示一个好的输入掩码。

Hide X:
INS DEL Entry:
            Mode: org-mode
            Regexp: a\|b
INS DEL Entry:
            Mode: text-mode
            Regexp: b\|c
INS
    State : STANDARD.
   Some variable
Groups: X

When I now do 当我现在这样做

(add-to-list 'x 1)

the mask becomes a significantly less user friendly 面具变得非常少用户友好

Hide x: 
'(1
  (org-mode . "a\\|b")
  (text-mode . "b\\|c"))
    State : CHANGED outside Customize. (mismatch)
   Some variable
Groups: X

Now of course I can include a fallback option by changing the definition to something like 现在我当然可以通过将定义更改为类似的内容来包含后备选项

(defcustom x 
  '((org-mode . "a\\|b")
    (text-mode . "b\\|c"))
  "Some variable"
  :group 'x
  :type '(repeat
          (choice
           (cons :tag "Entry"
                 (function :tag "Mode" :value text-mode)
                 (regexp))
           (sexp :tag "MISMATCHED ENTRY!"))))

which gives a customization mask 它提供了一个自定义掩码

Hide X:
INS DEL Choice: Value Menu MISMATCHED ENTRY!: 1
INS DEL Choice: Value Menu Entry:
            Mode: org-mode
            Regexp: a\|b
INS DEL Choice: Value Menu Entry:
            Mode: text-mode
            Regexp: b\|c
INS
    State : CHANGED outside Customize.
   Some variable
Groups: X

However this does now include an awkward drop-down menu that gives the user the choice between an entry and an invalid value. 但是,现在这包括一个笨拙的下拉菜单,让用户可以选择条目和无效值。 Rather I'd have the drop down value hidden by default and shown only when there is a mismatch with the valid option. 相反,默认情况下我会隐藏下拉值,只有在与有效选项不匹配时才会显示。 As a user my first thought seeing such would be along the lines of “ 作为一个用户我第一次想到这样的将是“ ”. ”。

Is there some standard way in the customization system to handle partly invalid values? 在自定义系统中是否有一些标准方法来处理部分无效值? I couldn't find any in the documentation.¹ 我在文档中找不到任何内容.¹


¹ http://www.gnu.org/software/emacs/manual/html_node/elisp/Customization-Types.html#Customization-Types ¹http //www.gnu.org/software/emacs/manual/html_node/elisp/Customization-Types.html#Customization-Types

You can edit (and save) the "raw sexp" displayed. 您可以编辑(并保存)显示的“原始性别”。 I agree it would be even better if the valid part were displayed using the usual widgets and only the invalid part is displayed as "raw sexp". 我同意如果使用通常的小部件显示有效部分并且只有无效部分显示为“raw sexp”会更好。 Patches welcome. 补丁欢迎。

You might want to use/implement your widget's :validate . 您可能想要使用/实现您的小部件:validate

From The Emacs Widget Library – 5 Basic Types (read with Ch im Widget RET m Basic RET ): 来自Emacs小部件库 - 5种基本类型(使用Ch im Widget RET m Basic RET读取):

':validate' A function which takes a widget as an argument, and returns 'nil' if the widget's current value is valid for the widget. ':validate'将小部件作为参数的函数,如果小部件的当前值对小部件有效,则返回'nil'。 Otherwise it should return the widget containing the invalid data, and set that widget's ':error' property to a string explaining the error. 否则,它应该返回包含无效数据的窗口小部件,并将该窗口小部件的':error'属性设置为解释错误的字符串。

The following predefined function can be used: 可以使用以下预定义函数:

 -- Function: widget-children-validate widget All the ':children' of WIDGET must be valid. 

In your example, all widgets in use should have :validate implemented, so you need only apply it. 在您的示例中,所有正在使用的小部件应该具有:validate applied,因此您只需要应用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM