Reputation: 355934
How do you list the active minor modes in emacs?
Upvotes: 124
Views: 36501
Reputation: 966
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!"))
Upvotes: 0
Reputation: 13457
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))
Upvotes: 2
Reputation:
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:
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).$buf
.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 <some mode variable>
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
.Enjoy! Onwards to greater and cleaner lisp code!
Upvotes: 4
Reputation: 18075
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.
Upvotes: 8
Reputation: 74480
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).
Upvotes: 25
Reputation: 4867
C-h m
or M-x describe-mode
shows all the active minor modes (and major mode) and a brief description of each.
Upvotes: 148