The Smart inference of indentation style section of the NoTabs page on EmacsWiki was very helpful. It shows you how to set up spaces for most projects, but switch to tabs if the file you're editing has more lines starting with tab than lines starting with space.
Here's the code:
(defun infer-indentation-style ()
;; if our source file uses tabs, we use tabs, if spaces spaces, and if
;; neither, we use the current indent-tabs-mode
(let ((space-count (how-many "^ " (point-min) (point-max)))
(tab-count (how-many "^\t" (point-min) (point-max))))
(if (> space-count tab-count) (setq indent-tabs-mode nil))
(if (> tab-count space-count) (setq indent-tabs-mode t))))
[in my c-mode hook, or whatever other mode I want to have smart indentation]
(setq indent-tabs-mode nil)
(infer-indentation-style)
This could still be an issue when editing new files that should always have tabs like makefiles. For those, your mode hook would just set it to tabs. For example:
(add-hook 'makefile-mode-hook
'(lambda()
(setq indent-tabs-mode t)
)
)