I often use .c file and .txt file on the split emacs frame.

I wrote a code such as the following to init.el.

;; change text-mode indent
(add-hook 'text-mode-hook
      (lambda ()
         (setq indent-line-function 'tab-to-tab-stop)
         (electric-indent-mode -1)

This code is convenience for me to arrange text on .txt file.

However, c-mode on emacs also doesn't indent for c-code.

So, I must input tab-key by oneself for .c file.

How can I write a setting to solve this problem.

Please teach me.

  • It's not clear to me what you're asking. Do you mean indentation does not work in c-mode? It should, try with emacs -Q. Or do you want the C source to be reindented automatically when the file is loaded? – JeanPierre Oct 1 '16 at 13:24

I'm not 100% sure what the question is, but I can see a problem:

electric-indent-mode is a global minor mode, so when your text-mode-hook function runs, it disables it globally.

C-hf electric-indent-mode says:

This is a global minor mode. To toggle the mode in a single buffer, use `electric-indent-local-mode'.

Therefore your code should be calling:

(electric-indent-local-mode -1)

n.b. Because you've used a lambda in a hook variable, you're liable to accidentally leave the original function in the hook when you add the modified version. If you're unsure, you should restart Emacs to make certain that the original call to the global mode isn't still happening. I recommend avoiding the use of lambdas in hook variables for this reason, and instead always using named functions:

(defun my-text-mode-hook ()
  "Custom behaviours for `text-mode'."
  (setq indent-line-function 'tab-to-tab-stop)
  (electric-indent-local-mode -1))

(add-hook 'text-mode-hook 'my-text-mode-hook)
| improve this answer | |

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.