How do you list active minor modes in emacs?

How do you specify active minor modes in emacs?

+80
emacs
Oct 02 '09 at 20:52
source share
5 answers

Ch m or Mx describe-mode displays all active small modes (and the main mode) and a brief description of each of them.

+88
Oct 02 '09 at 21:02
source share

A list of all minor mode commands is stored in the minor-mode-list variable. Finding out whether they are active or not is usually done by checking a variable with 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 minor modes can only be enabled in certain buffers).

+17
Oct 02 '09 at 21:17
source share

describe-mode can somehow find a list of allowed minor modes, why couldn't I? Therefore, after reading the source code, I realized that it receives a list of active minor modes from minor-mode-list and minor-mode-alist . Using a third-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 turn off all minor modes, use -each :

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

Remember to save the list of minor modes in a variable, otherwise you will have to restart Emacs or turn them on from memory.

+3
Dec 12 '14 at 16:21
source share

Here is a simple alternative snippet, similar to some of the methods that were already covered 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)) 
+1
Jan 18 '17 at 1:16
source share

If you want to programmatically do something with all buffers that have a certain mode, then the best, most minimalistic, cleanest, built-in solution looks like this:

 (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 performs the following actions:

  • Get a list of all buffers via buffer-list , with the current active buffer at the top of the list (therefore, it usually processes what you want first, but do not consider the current-buffer parameter if you don't care).
  • Scroll through the buffer list and assign $buf each buffer name.
  • Use with-current-buffer $buf to tell Emacs that all the code inside the body should work as if it were running inside the $buf buffer instead of what you actually display on the screen.
  • when <some mode variable> is the right way to check if the mode is on; you can also use if and other similar methods. In any case, the goal is to check whether the main mode or main mode variable is set in the buffer. Almost all modes define a variable by “defining” a mode, which automatically forces them to create a local buffer variable, named after the mode, how it works. And if they don’t have a standard variable, look at your own source code to see how their “switch” code determines how to turn them on and off. 99% of them use the existence of their name variable (and if they do not, I suggest reporting this as an error for the author of the mode). For example, to check if a buffer has an active mode in the form of a space, you should say when whitespace-mode .
  • After that, it simply displays the message in the message buffer with "x" and the name of the buffer in which the mode is active. Where you put your own code, do what you wanted to do with an open buffer.

Enjoy it! Go to a larger and more understandable lisp code!

0
Dec 11 '16 at 21:58
source share



All Articles