在Emacs中,如何在文件中为所有可能的值设置局部变量

hal*_*leo 17 variables emacs elisp

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

(defvar leo-special-var "")
(make-variable-buffer-local 'leo-special-var)
Run Code Online (Sandbox Code Playgroud)

现在我将这个变量设置在带有行的文件中(在要编辑的文件中):

# Local Variables:
# leo-special-var: "-d http://www.google.com.au"
# End:
Run Code Online (Sandbox Code Playgroud)

我想把这个变量视为" 对所有值都是安全.这就是为什么safe-local-variable-values没有帮助.相反,我试过(在lisp代码中):

# setting the symbol property of the variable
(put 'leo-special-var 'safe-local-variable 'booleanp)
Run Code Online (Sandbox Code Playgroud)

但没有成功.设置符号属性时我做错了吗?或者还有另一种方式吗?

Ste*_*fan 19

你想用

(put 'leo-special-var 'safe-local-variable #'stringp)
Run Code Online (Sandbox Code Playgroud)

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


Dre*_*rew 11

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

(put 'leo-special-var 'safe-local-variable (lambda (_) t))
Run Code Online (Sandbox Code Playgroud)

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

(但我认为您可能希望声明变量对任何值都是安全的.)

  • 这不是我*想要的*(我想要一个仅使其对字符串安全的解决方案),而是我*要求*的目的。你好,@ Drew! (2认同)

phi*_*ils 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::).
Run Code Online (Sandbox Code Playgroud)

  • 我知道这个文档,但我不完全理解它:我应该用“stringp”替换“boleanp”,因为“leo-special-var”myvariable是一个字符串变量吗? (2认同)