Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 772
cadrian Avatar asked Sep 08 '11 14:09

cadrian


2 Answers

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)
like image 163
cadrian Avatar answered Sep 22 '22 13:09

cadrian


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))
like image 32
Arthur Azevedo De Amorim Avatar answered Sep 22 '22 13:09

Arthur Azevedo De Amorim