This is what I use personally, and I think it meets all your indentation needs, you can try it by adding code to your .emacs file. This function takes care of the correct indentation when I write the code:
(defun linux-c-indent ()
"adjusted defaults for C/C++ mode use"
(interactive)
(setq tab-width 8)
(setq indent-tabs-mode nil) ;;force spaces, to work with dumber editors
(setq c-basic-offset 8)
(setq c-set-style "K&R"))
(add-hook 'c-mode-common-hook 'linux-c-indent)
And if I want to retype files written by other people, I use the following:
(defun iwb ()
"indent whole buffer"
(interactive)
(delete-trailing-whitespace)
(indent-region (point-min) (point-max) nil)
(untabify (point-min) (point-max)))
The iwb function runs every time I open the "programming" file in emacs. Thus, I always see well-coded code. I can put this bit of code too, I want this.
source
share