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

标签 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 RETmatchAgainstDirectory 不会显示为有效的变量名。这是为什么?

我知道我可以执行 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.

关于emacs - 在 emacs 中设置一个新的全局变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7017936/

相关文章:

emacs - [control shift up] 不起作用

如果文件有自动保存数据,emacs-daemon 启动会卡住

emacs - 如何让emacs从初始目录自动加载和保存桌面?

emacs - 一个简单的 lisp 程序

emacs - 关闭 Emacs 中除当前缓冲区之外的所有缓冲区

emacs - symbol-plist 返回具有值的 elisp 符号的列表

emacs - 有哪些好的 emacs 字体可以手动设置?

function - emacs lisp 是否支持在词法上重新定义函数?

emacs - 如何处理冲突的键绑定(bind)

emacs - 如何在 Emacs 组织模式下查看新缓冲区中的 "Notes"?