cadrian
cadrian

Reputation: 7376

Different tab indent settings in different modes

I'm currently using whitespace-cleanup in my save hook. Using indent-tabs-mode, I'm able to save files without any tabs.

All is well, I don't want tabs in my files. But.

Makefiles do need tabs. That is my problem. How do I change my settings for makefile-mode?

I tried to setq either indent-tabs-mode (the doc says it becomes buffer-local) or whitespace-style, it does not work.

Upvotes: 7

Views: 1768

Answers (3)

Noah
Noah

Reputation: 34343

The best solution I have found for whitespace is ethan-wspace. It cleans up any whitespace that you made dirty yourself but leaves other whitespace intact.

It works for tabs in makefiles and avoids the messy diff problem where there are lots of diff lines that are just whitespace changes

Upvotes: 0

Arthur Azevedo De Amorim
Arthur Azevedo De Amorim

Reputation: 23622

I've had the same problem, and it seems that this is a bug in Emacs (as of 24.2). Try this, using the following .emacs:

(setq-default indent-tabs-mode nil)
(add-hook 'after-save-hook 'whitespace-cleanup)

If you open a file, save it, and then open a Makefile, you'll have the problem you described. But if you open a Makefile first, save it, and then open another type of file, you'll have the opposite problem: 8 spaces will be replaced by tabs.

The problem is that indent-tabs-mode is buffer-local, but in whitespace.el it is set to a regular variable called whitespace-indent-tabs-mode. Hence, the first value that's seen is the one that counts.

Here's another workaround that solves some other problems too. Add this to your .emacs:

(defadvice whitespace-cleanup (around whitespace-cleanup-indent-tab
                                      activate)
  "Fix whitespace-cleanup indent-tabs-mode bug"
  (let ((whitespace-indent-tabs-mode indent-tabs-mode)
        (whitespace-tab-width tab-width))
    ad-do-it))

Upvotes: 2

cadrian
cadrian

Reputation: 7376

OK, my bad. Files were loaded before changing the mode.

The following code works fine, provided it is loaded in the .emacs before opening any file (I have my own project manager which re-opens last files).

(defun my-tabs-makefile-hook ()
  (setq indent-tabs-mode t))
(add-hook 'makefile-mode-hook 'my-tabs-makefile-hook)

Upvotes: 3

Related Questions