20

在 Elisp 中,我为特殊的自定义模式引入了一个变量,例如:

(defvar leo-special-var "")
(make-variable-buffer-local 'leo-special-var)

现在我在文件中设置这个变量,我用这些行(在要编辑的文件中):

# Local Variables:
# leo-special-var: "-d http://www.google.com.au"
# End:

而且我想将此变量视为“对其所有值都是安全的。这就是为什么safe-local-variable-values没有帮助。相反,我尝试了(在 lisp 代码中):

# setting the symbol property of the variable
(put 'leo-special-var 'safe-local-variable 'booleanp)

但没有成功。设置符号属性时我做错了吗?还是有其他方法?

4

3 回答 3

26

你想用

(put 'leo-special-var 'safe-local-variable #'stringp)

说它是安全的,只要它是一个字符串。

于 2013-11-06T13:20:20.223 回答
13

如果你真的想声明它对所有值都是安全的,那么使用这个:

(put 'leo-special-var 'safe-local-variable (lambda (_) t))

此处测试安全性的函数返回非nil任何值。

(但我认为您可能不想声明变量对于任何值都是安全的。)

于 2013-11-06T17:02:04.910 回答
5

它在手册中:(elisp) File Local Variables

   You can specify safe values for a variable with a
`safe-local-variable' property.  The property has to be a function of
one argument; any value is safe if the function returns non-`nil' given
that value.  Many commonly-encountered file variables have
`safe-local-variable' properties; these include `fill-column',
`fill-prefix', and `indent-tabs-mode'.  For boolean-valued variables
that are safe, use `booleanp' as the property value.  Lambda
expressions should be quoted so that `describe-variable' can display
the predicate.

   When defining a user option using `defcustom', you can set its
`safe-local-variable' property by adding the arguments `:safe FUNCTION'
to `defcustom' (*note Variable Definitions::).
于 2013-11-06T08:04:55.657 回答