ChatGPT解决这个技术问题 Extra ChatGPT

How do you list the active minor modes in emacs?

How do you list the active minor modes in emacs?


P
Phil

C-h m or M-x describe-mode shows all the active minor modes (and major mode) and a brief description of each.


s
sw1nn

A list of all the minor mode commands is stored in the variable minor-mode-list. Finding out whether they're active or not is usually done by checking the variable of the same name. So you can do something like this:

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

Note: this only works for the current buffer (because the minor modes might be only enabled in certain buffers).


add-to-list inside map? convoluted.
@jrockway Not my proudest lisp moment.
Using boundp instead of symbolp you can get rid of the condition-case.
M
Mirzhan Irkegulov

describe-mode can somehow come up with a list of enabled minor modes, why couldn't I? So after reading its source code I realized that it gets the list of active minor modes from both minor-mode-list and minor-mode-alist. Using 3rd-party dash.el list manipulation library I came with this code:

(--filter (and (boundp it) (symbol-value it)) minor-mode-list)

So, for example, to disable all minor modes, use -each:

(--each (--filter (and (boundp it) (symbol-value it)) minor-mode-list)
        (funcall it -1))

Don't forget to save the list of minor modes in a variable, otherwise you would have to restart Emacs or enable them by memory.


g
gw0

If you want to programmatically do something with all buffers that have a certain mode active, then the best, most minimalistic, cleanest, built-in solution is as follows:

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

It does the following:

Retrieve a list of all buffers via buffer-list, with the currently active buffer at the head of the list (so it's treated first, usually what you want, but leave out the current-buffer parameter if you don't care). Loop through the buffer list and assign each buffer name to the variable $buf. Use with-current-buffer $buf to tell Emacs that all code within the body should run as if it was running inside buffer $buf instead of whatever buffer you're really displaying on screen. when is the correct way to check if a mode is enabled; you can also use if and other such methods. Either way, the goal is to check if a minor or major-mode's main mode variable is set in the buffer. Almost all modes define a variable via "defining" a mode, which automatically causes them to create a buffer-local variable named after the mode, which is how this works. And if they don't have a standard variable, look at their own source code to see how their "toggle" code determines how to toggle them on and off. 99% of them use the existence of their modename's variable (and if they don't, I suggest reporting that as a bug to the mode's author). For example, to check if a buffer has whitespace-mode active, you would say when whitespace-mode. After that, it just outputs a message to the Messages buffer, with an "x" and the name of the buffer that had the mode active. That's where you'd put your own code, to do whatever you wanted to do with the discovered buffer.

Enjoy! Onwards to greater and cleaner lisp code!


l
lawlist

Here is a simple alternative snippet similar to some of the methods that have already been addressed in other answers:

(delq nil
  (mapcar
    (lambda (x)
      (let ((car-x (car x)))
        (when (and (symbolp car-x) (symbol-value car-x))
          x)))
    minor-mode-alist))

a
aBarocio80

If you just want to know if a particular minor mode (say, evil-mode) is active in the buffer, you could evaluate the following:

(when (member 'evil-mode minor-mode-list)
   (message "`evil-mode' is active!"))