The global electric-indent-mode
does not provide an exclusion list where you could add fundamental-mode
and txt-mode
.
Use electric-indent-local-mode
in major mode hooks instead of the global electric-indent-mode
.
The first "normal" approach is to switch the minor-mode actively on in all major-modes you want it to have: Call electric-indent-local-mode
in the hooks of the major modes where you want to switch it on:
(with-eval-after-load 'electric
(electric-indent-mode -1))
(add-hook 'c-mode-hook #'electric-indent-local-mode)
Alternative to c-mode-hook
you can also use prog-mode-hook
to switch electric-indent-mode
on in almost all modes for programming languages.
The second version is to call electric-indent-local-mode
in after-change-major-mode-hook
if the major-mode is not in some exclusion list.
(with-eval-after-load 'electric
(electric-indent-mode -1))
(defcustom my-electric-indent-exclusion-list '(txt-mode fundamental-mode)
"Major modes where `electric-indent-local-mode' is not switched on."
:group 'my
:type '(repeat symbol))
(defun my-switch-on-electric-indent ()
"Maybe switch on `electric-indent-local-mode'.
Do not activate electric indent if the major mode is
in `my-electric-indent-exclusion-list'."
(unless (apply #'derived-mode-p my-electric-indent-exclusion-list)
(electric-indent-local-mode)))
(add-hook 'after-change-major-mode-hook #'my-switch-on-electric-indent)
As Drew indicated in his comment, if it was not for fundamental mode, there would be another simple method to disable electric-indent-mode
for specific major modes.
For most minor modes that have a global and a local version and where there is not an explicite exclusion list, it is possible to switch on the global version and switch it off with the local minor mode version in the major-modes hook.
This cannot be used for fundamental-mode
since there is no fundamental-mode-hook
variable.
I demonstrate it here for text-mode
:
(electric-indent-mode)
(add-hook 'text-mode-hook
(lambda ()
(electric-indent-local-mode -1)))