How to check whether a minor mode(.e.g flymake-mode) is on?
Asked Answered
D

2

34

I always get error when use flymake-mode. And flymake-mode turn off.

enter image description here

So I want to check whether flymake-mode is on. If it is not on, I will turn it on before call flymake-next-error.

Thanks.

Dupion answered 10/4, 2012 at 11:47 Comment(3)
If you're getting an error when you turn it on, what do you plan to gain from checking?Op
According to its homepage (flymake.sourceforge.net), flymake is a minor mode, not a major mode.Collywobbles
Thanks. The error does not occurre all the time. It occurred When my js file has a lot of failed. And flymake is a minor mode.Dupion
F
51

Most minor modes are defined using the define-minor-mode macro. For buffer-local modes, that macro creates a buffer-local variable of the same name as the mode itself1. The variable is non-nil when the mode is enabled for the current buffer.

Therefore to check whether flymake-mode is enabled, you can test the variable flymake-mode (noting that this variable might not be defined at all if the mode has not been enabled):

(and (boundp 'flymake-mode) flymake-mode)

Which, as Thom point out in the comments, can be expressed using the convenient macro:

(bound-and-true-p flymake-mode)

Which we can use as the condition for an if or when or unless expression:

(if (bound-and-true-p flymake-mode)
    (message "flymake-mode is on")
  (message "flymake-mode is off"))

1 For global minor modes, the corresponding variable is global; but flymake-mode is buffer-local.

Freya answered 10/4, 2012 at 12:40 Comment(3)
Thanks. It is my mistake. Flymake is not a major mode, but a minor mode.Dupion
The test here can be shortened to just (bound-and-true-p flymake-mode).Atween
M-: for opening a minibuffer where you can evaluate the expression.Adjudication
U
4

Another solution is to use M-x describe-mode to show all the active minor modes (and major mode) and a brief description of each.

If the minor mode is on in the current buffer, you can find it's name on the popup buffer.

Updo answered 7/4, 2019 at 2:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.