如何在Emacs Lisp中切换命令?

发布于 2025-01-28 16:07:54 字数 392 浏览 3 评论 0原文

我想拥有一个可以切换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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫卆 2025-02-04 16:07:54

描述功能ch f)在display-line-numbers-mode给出以下内容:

Signature:
(display-line-numbers-mode &optional ARG)

Documentation:
[...]
This is a minor mode.  If called interactively, toggle the
Display-Line-Numbers mode mode. [...]

If called from Lisp, toggle the mode if ARG is toggle.  Enable
the mode if ARG is nil, omitted, or is a positive number.
Disable the mode if ARG is a negative number.

这是由定义少模式宏,对于大多数次要模式,先前的文档将完全相同。

我们从中得到的:

  • 当互动称为命令时,使用例如mx display-line-numbers-mode),这是一个切换。
  • 当从 elisp源代码调用时,您需要指定一个参数。

如果您希望它充当切换,请在代码中使用(display-line-numbers-mode'toggle)。如果您想要比简单切换更先进的东西(例如,检查一些额外的条件),请使用1和-1作为参数分别启用和禁用模式(顺便说一句,尽管文档不清楚,但0算作负面 了模式。

Number shere ,so (display-line-numbers-mode 0)禁用 知道。

describe-function (C-h f) on display-line-numbers-mode gives the following:

Signature:
(display-line-numbers-mode &optional ARG)

Documentation:
[...]
This is a minor mode.  If called interactively, toggle the
Display-Line-Numbers mode mode. [...]

If called from Lisp, toggle the mode if ARG is toggle.  Enable
the mode if ARG is nil, omitted, or is a positive number.
Disable the mode if ARG is a negative number.

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:

  • When called interactively (i.e. as a command, using for example M-x display-line-numbers-mode) this acts as a toggle.
  • When called from Elisp source code, you need to specify an argument.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文