emacselispmajor-mode

How to turn off electric-indent-mode for specific Major mode?


I have few Major modes (like: Yaml and NXML) that I don't want electric-indent-mode (I want it for C like languages) but I'm unable to turn if off. To enable I have:

(electric-indent-mode 1)

from documentation (for variable electric-indent-mode)

Non-nil if Electric-Indent mode is enabled. See the command electric-indent-mode' for a description of this minor mode. Setting this variable directly does not take effect; either customize it (see the info nodeEasy Customization') or call the function `electric-indent-mode'.

and for a function

Toggle on-the-fly reindentation (Electric Indent mode). With a prefix argument ARG, enable Electric Indent mode if ARG is positive, and disable it otherwise. If called from Lisp, enable the mode if ARG is omitted or nil.

so I try to turn it off in a hook:

(add-hook 'yaml-mode-hook (lambda ()                        
                             (electric-indent-mode -1)))

(Actualy I use after-change-major-mode-hook and check (memql major-mode '(yaml-mode python-mode nxml-mode)) where I can add more modes to the list).

But it don't work, I've also try:

(set (make-local-variable 'electric-indent-mode) nil)

No joy. But it work when I eval (electric-indent-mode -1) from .emacs files.


Solution

  • With a recent Emacs (probably Emacs snapshot only) you can use electric-indent-local-mode, e.g.:

    (add-hook 'yaml-mode-hook (lambda () (electric-indent-local-mode -1)))
    

    If your Emacs lacks this function, you can still sort of disable the mode via electric-indent-functions, e.g.

    (add-hook 'yaml-mode-hook
              (lambda ()
                 (add-hook 'electric-indent-functions
                                (lambda () 'no-indent) nil 'local)))
    

    And in either case, you may probably want to restore C-j, via

    (add-hook 'yaml-mode-hook 
              (lambda () (local-set-key (kbd "C-j") #'newline-and-indent)))