【问题标题】:In Emacs how do I make a local variable safe to be set in a file for all possible values在 Emacs 中,如何使局部变量安全地在文件中设置为所有可能的值
【发布时间】:2013-11-06 07:16:04
【问题描述】:

在 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)

但没有成功。设置符号属性时我做错了吗?还是有别的办法?

【问题讨论】:

    标签: variables emacs elisp


    【解决方案1】:

    你想用

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

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

    【讨论】:

      【解决方案2】:

      如果您真的想声明 所有 值是安全的,请使用:

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

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

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

      【讨论】:

      • 这不是我想要的(我想要一个只对字符串安全的解决方案),但这正是我要求的。你好,@Drew!
      • 我建议不要使用它,因为它在不安全方面会犯错误。
      • @stefan:我也建议不要这样做。但问题是如何做到这一点。
      【解决方案3】:

      手册中有:(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::).
      

      【讨论】:

      • 我知道这个文档,但我不完全理解:我应该用stringp 替换'boleanp,因为leo-special-varmyvariable 是一个字符串变量吗?
      • 是的,stringp 将是您所需要的。如果您查看booleanp 的帮助,您会看到它为tnil 的值返回t only,所以它会为所有值返回nil您使用过,因此表明它们不安全。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多