Theresa O’Connor

Using tabs when you have to

Despite the fact that using literal tab characters is clearly wrong, there are those in the world who insist on using them, and chances are you'll find yourself collaborating with such people.

In a recent post to emacs-devel, Stefan Monnier suggested a really nice heuristic to have Emacs guess the appropriate value of indent-tabs-mode to use:

For some file types indent-tabs-mode should always be t (e.g. for ChangeLog and Makefile) and for others it should always be nil (e.g. for LaTeX and TeXinfo).

How 'bout:

(add-hook 'find-file-hook
  (lambda ()
    (if (and (null indent-tabs-mode)
             (local-variable-p 'indent-tabs-mode) ; Trust the major mode.
             (save-excursion
               (goto-char (point-min))
               ;; If there are at least 10 lines with a leading TAB, use TABs.
               (re-search-forward "^    " (+ (point) 100000) t 10)))
        (set (make-local-variable 'indent-tabs-mode) t))))