如何在Emacs Lisp中切换命令?
我想拥有一个可以切换Emacs中线号的快捷方式。
这就是我到目前为止的:
(defun my-toggle-display-line-numbers-mode-function ()
"Toggles the line numbers"
(interactive)
(display-line-numbers-mode)
)
(global-set-key [(f7)] 'my-toggle-display-line-numbers-mode-function)
但是它只会打开行号。我无法将其关闭,与我使用MX Display-line-numbers-Mode
不同,它将打开或关闭毫无问题。
知道如何改进我的脚本以便按预期运行吗?
I would like to have a shortcut that would toggle the showing of line numbers in Emacs.
This is what I have so far:
(defun my-toggle-display-line-numbers-mode-function ()
"Toggles the line numbers"
(interactive)
(display-line-numbers-mode)
)
(global-set-key [(f7)] 'my-toggle-display-line-numbers-mode-function)
But it will only turn on the line numbers. I can't turn it off, unlike when I use M-x display-line-numbers-mode
which will turn on or off without any problem.
Any idea how to improve my script so it works as intended?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
描述功能
(ch f
)在display-line-numbers-mode
给出以下内容:这是由
定义少模式宏,对于大多数次要模式,先前的文档将完全相同。
我们从中得到的:
mx display-line-numbers-mode
),这是一个切换。如果您希望它充当切换,请在代码中使用
(display-line-numbers-mode'toggle)
。如果您想要比简单切换更先进的东西(例如,检查一些额外的条件),请使用1和-1作为参数分别启用和禁用模式(顺便说一句,尽管文档不清楚,但0算作负面 了模式。Number shere ,so
(display-line-numbers-mode 0)
禁用 知道。describe-function
(C-h f
) ondisplay-line-numbers-mode
gives the following:This is the automatic desciption produced by the
define-minor-mode
macro, and for most minor modes, the previous documentation will be exactly the same.What we get from this:
M-x display-line-numbers-mode
) this acts as a toggle.If you want it to act as a toggle, use
(display-line-numbers-mode 'toggle)
in your code. If you want something slightly more advanced than a simple toggle (for example, checking some extra conditions), use 1 and -1 as arguments to respectively enable and disable the mode (incidentally, and although the documentation is unclear, 0 counts as a negative number here, and so(display-line-numbers-mode 0)
disables the mode.As it is often the case, Emacs built-in documentation shows all there is to know. Try to get familiar with the help system, it's really good compared to a lot of softwares.