在emacs中设置新全局变量的值

时间:2011-08-10 21:17:01

标签: emacs elisp

我在函数中有这一行:

(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 ()),但我正在寻找一种更短的方法。这样的事情存在吗?

2 个答案:

答案 0 :(得分:4)

要在执行 M-x set-variable 时显示变量,必须通过defvar定义,因为(如the docs say)

  

M-x set-variable仅限于用户选项变量,但您可以设置   任何带有Lisp表达式的变量,使用函数setq。

因此,将其定义为变量:

(defvar matchAgainstDirectory nil "some comment") 

(defvar matchAgainstDirectory) ; value and comment are optional

所以,将它放在代码中的顶层(不在let语句中)。

setq不创建用户选项变量,它只是创建一个emacs lisp变量 - 用户不能通过 Mx set-variable 手动设置。

答案 1 :(得分:2)

你也可以使用 M - : (setq matchAgainstDirectory nil) RET ,这比调用ielm略短。