【问题标题】:How to figure out whether or not a particular mode is currently enabled using Emacs in Elisp?如何确定当前是否在 Elisp 中使用 Emacs 启用了特定模式?
【发布时间】:2021-02-20 20:53:02
【问题描述】:

我想写一个 Elisp 脚本

  • 打开auto-save-visited-mode(如果它已关闭),然后
  • 如果它已经打开,什么也不做。

如何查看 Elisp 中当前是否启用了此模式?

更新 1:M-x describe-mode 显示所有启用的次要模式的列表。

更新 2: 根据this answer,您可以使用此代码显示所有活动次要模式的列表:

(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)))

(which-active-modes)

更新 3: 似乎可以使用的最终版本:

(if auto-save-visited-mode
    (message "The mode is on")
    (message "The mode is off")
)

【问题讨论】:

    标签: emacs mode


    【解决方案1】:

    您可以通过简单地评估变量 auto-save-visited-mode 来测试它是否启用,如其文档中所述,C-h vauto-save-visited-mode

    要启用它,只需致电(auto-save-visited-mode 1)。这将启用它或什么都不做。

    编辑: (auto-save-visited-mode 1) 本身可能会完成您想要的,但它确实会运行设置代码/再次调用模式挂钩,即使它之前已设置。因此,您可以使用(unless auto-save-visited-mode (auto-save-visited-mode)) 来避免这种情况。

    【讨论】:

      【解决方案2】:

      如果模式打开,则其模式变量的值不是nil。如果关闭,则值为nil

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-16
        • 2011-12-03
        • 1970-01-01
        • 1970-01-01
        • 2011-04-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多