defcustom 怎样定义一个值的列表以供选择?

defcustom 的 :options 有什么用,定义了哪里都看不到

Edit:

我的实际使用场景是定义一个变量,它的值是 symbol 类型,提供一个值的列表以供选择,这个列表可以用于 counsel-set-variable 命令。

customize-groups 命令后,输入defcustom的 :group 的名字就可以看到,配置以后按 Ctrl + X Ctrl + S 会自动保存选项到 ~/.emacs

看不到,难道我设置的方法不对?

定义完了应该可以选择的呀

不行,我刚才看了一下manual,:options 只对 hook alist 等类型有效,我也是想定义一些备选项,我的变量的值是 symbol 类型的。

你确定嘛,我看过别的包的源码里 有用symbol的啊。

This is meaningful only for certain types, currently including hook, plist and alist. See the definition of the individual types for a description of how to use :options.

来源: Variable Definitions (GNU Emacs Lisp Reference Manual)

正确的用法应该是用 :type 关键词,创建一个复合类型:

(defcustom my-custom-variable nil
  "My choices."
  :type '(choice
          (const 'a)
          (const 'b)
          (const 'c)
          (const 'd))
  :group 'abc)

注意列表以及 a b c d 这些 symbol 都 quote 了,不然会报错

对了,你没猜错,这样定义了之后就可以用 counsel-set-variable 来设置变量了,设置的值都成为备选项。

这是 prog-mode-hook 的源代码:

(defcustom prog-mode-hook nil
  "Normal hook run when entering programming modes."
  :type 'hook
  :options '(flyspell-prog-mode abbrev-mode flymake-mode linum-mode
                                prettify-symbols-mode)
  :group 'prog-mode)

然后 M-x customize-option prog-mode-hook 会提供这些选项让用户选择,而不必手动输入函数名

2 个赞

hook 还没有试,谢谢指点

这个貌似和我的使用场景不合,hook 的值必须是函数的列表

‘hook’ The value must be a list of functions. This customization type is used for hook variables. You can use the ‘:options’ keyword in a hook variable’s ‘defcustom’ to specify a list of functions recommended for use in the hook; *Note Variable Definitions::.

我所定义的变量的值是 Symbol

Edit

我帖子的标题看来是有一定误导性,抱歉

Edit 2

帖子标题和内容已修改