3

Is there any function do disable all the minor modes if I enable a specific major mode (let's take the text-mode as an example)?

  • Enabling a major mode normally begins by disabling all buffer-local minor modes. So please be more specific. – Stefan Jun 25 '18 at 20:34
  • 1
    I am guessing that you only want to disable the buffer-local minor modes (and not the global minor modes). To my knowledge Emacs does not provide a convenient way to identify an active mode as either buffer-local or global, but you could likely use (buffer-local-variables &optional BUFFER) and check each of the minor-mode-list values against that list. Note that globalized modes act in after-change-major-mode-hook so you can't simply disable minor modes in the major mode hook, as that will be too early. – phils Jun 25 '18 at 20:59
4

You can use a function like this:

(defun disable-all-minor-modes ()
  (interactive)
  (mapc
   (lambda (mode-symbol)
     (when (functionp mode-symbol)
       ;; some symbols are functions which aren't normal mode functions
       (ignore-errors 
         (funcall mode-symbol -1))))
   minor-mode-list))

Note that this will probably disable more functionality than you think.

If you want to disable minor modes when you in a specific mode, you can just hook this function like this:

(add-hook 'lisp-mode-hook #'disable-all-minor-modes)

Simply replace lisp-mode with the major mode you want to use, and keep in mind that this will run in derived major modes as well.

  • 2
    Disabling modes in the major mode hook is too early. Globalized minor modes enable their buffer-local mode in after-change-major-mode-hook, which runs later. You can instead use the major mode hook to (add-hook 'after-change-major-mode-hook 'FUNC :append :local) – phils Jun 25 '18 at 21:00
  • I haven't thought about that. Thanks, @phils. – DoMiNeLa10 Jun 26 '18 at 16:12

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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