您如何列出 emacs 中的活动次要模式?
C-h m
或 M-x describe-mode
显示所有活动的次要模式(和主要模式)以及每种模式的简要说明。
所有次要模式命令的列表存储在变量 minor-mode-list
中。通常通过检查同名变量来确定它们是否处于活动状态。所以你可以做这样的事情:
(defun which-active-modes ()
"Give a message of which minor modes are enabled in the current buffer."
(interactive)
(let ((active-modes))
(mapc (lambda (mode) (condition-case nil
(if (and (symbolp mode) (symbol-value mode))
(add-to-list 'active-modes mode))
(error nil) ))
minor-mode-list)
(message "Active modes are %s" active-modes)))
注意:这仅适用于当前缓冲区(因为次要模式可能仅在某些缓冲区中启用)。
describe-mode
可以以某种方式提出启用的次要模式列表,为什么我不能?因此,在阅读了它的源代码后,我意识到它从 minor-mode-list
和 minor-mode-alist
中获取了活动次要模式的列表。使用 3rd-party dash.el
列表操作库,我附带了以下代码:
(--filter (and (boundp it) (symbol-value it)) minor-mode-list)
因此,例如,要禁用所有次要模式,请使用 -each
:
(--each (--filter (and (boundp it) (symbol-value it)) minor-mode-list)
(funcall it -1))
不要忘记将次要模式列表保存在变量中,否则您将不得不重新启动 Emacs 或通过内存启用它们。
如果您想以编程方式对具有特定模式的所有缓冲区执行某些操作,那么最好、最简约、最干净的内置解决方案如下:
(dolist ($buf (buffer-list (current-buffer)))
(with-current-buffer $buf
(when some-buffer-local-minor-or-major-mode-variable-you-want-to-find
(message "x %s" $buf))))
它执行以下操作:
通过 buffer-list 检索所有缓冲区的列表,当前活动的缓冲区位于列表的头部(因此首先处理它,通常是您想要的,但如果您不关心,请忽略 current-buffer 参数)。循环遍历缓冲区列表并将每个缓冲区名称分配给变量 $buf。使用 with-current-buffer $buf 告诉 Emacs 正文中的所有代码都应该像在缓冲区 $buf 中运行一样运行,而不是在屏幕上实际显示的任何缓冲区中运行。当
享受!朝着更大更干净的 lisp 代码前进!
这是一个简单的替代片段,类似于其他答案中已经解决的一些方法:
(delq nil
(mapcar
(lambda (x)
(let ((car-x (car x)))
(when (and (symbolp car-x) (symbol-value car-x))
x)))
minor-mode-alist))
如果您只想知道某个特定的次要模式(例如 evil-mode
)是否在缓冲区中处于活动状态,您可以评估以下内容:
(when (member 'evil-mode minor-mode-list)
(message "`evil-mode' is active!"))
不定期副业成功案例分享
boundp
而不是symbolp
您可以摆脱condition-case
。