我在函数中有这一行:
(let (v1) (v2)
... more code here ...
(setq matchAgainstDirectory (
read-directory-name "Set directory to match against:")))))))
matchAgainstDirectory
不在 let
声明的值列表中,所以我假设它将设置一个全局值。
我想在某个时候将它设置为 nil
,但是如果我按 M-x RET set-variable RET
,matchAgainstDirectory
不会显示为有效的变量名。这是为什么?
我知道我可以执行 M-x ielm
,并在那里写 (setq matchAgainstDirectory ())
,但我正在寻找一种更短的方法来执行此操作。有这种东西吗?
最佳答案
要在您执行M-x set-variable 时显示变量,必须通过defvar
定义它。因为(如 the docs say) :
M-x set-variable is limited to user option variables, but you can set any variable with a Lisp expression, using the function setq.
因此,将其定义为变量:
(defvar matchAgainstDirectory nil "some comment")
或
(defvar matchAgainstDirectory) ; value and comment are optional
所以,把它放在你的代码中顶层的某个地方(而不是在你的 let
语句中)。
setq
不创建用户选项变量,它只是创建一个 emacs lisp 变量 - 不打算由用户通过 M-x set-variable手动设置kbd>.
关于emacs - 在 emacs 中设置一个新的全局变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7017936/