2

Is there a way to programatically detect (in Elisp) if a minor mode is a global minor mode or a local minor mode?

For instance I'd like to be able to detect that show-paren-mode is a global minor mode and that smart-dash-mode is a local minor mode.

1

That information isn't stored, unfortunately.

You can check whether the mode variable show-paren-mode is global or buffer-local:

(local-variable-if-set-p 'show-paren-mode)

That's not a guarantee (there's nothing stopping you giving a global mode a buffer-local value), but it's a reasonable heuristic.

You might deal with that issue with:

(with-temp-buffer
  (local-variable-if-set-p 'show-paren-mode))
2
  • Thanks. I assume it could also be checked with local-variable-p. Both of these are implemented in C and have an optional BUFFER argument, at least back to Emacs 26.3, not sure before.
    – PRouleau
    Sep 15 '21 at 17:07
  • If you're only dealing with buffers where the mode is enabled, then local-variable-p would be as good, yes.
    – phils
    Sep 16 '21 at 0:19
0

In addition to Phils' answer, I noted that:

  • When checking if a minor-mode is local or global inside a function invoked for a major-mode by a hook, the code must first check if the mode variable is bound before doing the check with either local-variable-if-set-p or local-variable-p. Otherwise if the minor-mode is auto-loaded and not yet loaded, then both local-variable-if-set-p and local-variable-p return nil even if the minor-mode is a local minor-mode.

I wanted to verify the validity of automatic minor-mode activation done by a hook for major-mode and generate a warning if a global minor-mode was activated via a hook meant to activate only local minor-modes (and vice-versa). If the minor-mode is auto-loaded and a major-mode is activated early (like when you start Emacs and open a file in that major mode right away) then the check would fail and warning generation was not reliable.

Ideally the code would also detect that the variable is auto-loaded, load the file and check again. But that's out of the scope of the current question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.