簡體   English   中英

如何確定當前是否在 Elisp 中使用 Emacs 啟用了特定模式?

[英]How to figure out whether or not a particular mode is currently enabled using Emacs in Elisp?

我想寫一個 Elisp 腳本

  • 打開auto-save-visited-mode ,如果它是關閉的,並且
  • 如果它已經打開,什么也不做。

如何確定 Elisp 目前是否啟用了此模式?

更新 1: Mx describe-mode顯示所有啟用的次要模式的列表。

顯示次要模式列表的屏幕截圖

更新 2:根據這個答案,您可以使用以下代碼顯示所有活動次要模式的列表:

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

您可以通過簡單地評估變量auto-save-visited-mode來測試它是否啟用,如其文檔Ch v auto-save-visited-mode中所述。

要啟用它,只需調用(auto-save-visited-mode 1) 這將啟用它或什么都不做。

編輯: (auto-save-visited-mode 1)本身可能會完成你想要的,但它確實會運行設置代碼/再次調用模式掛鈎,即使它之前已設置。 因此,您可以使用(unless auto-save-visited-mode (auto-save-visited-mode))來避免這種情況。

如果模式打開,則其模式變量的值nil 如果它關閉,則值為nil

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM