views:

561

answers:

6

I'm a Linux user looking for a code beautifier which will take files containing C code and format them to specification. Specifically, I'm looking to:

  • Change all indentations to be 8 spaces
  • Format blocks of code consistently
  • Add line breaks consistently

It would be nice if it had both defaults and the ability to customize. I prefer free / open source solutions when available. Any suggestions?

UPDATE: I should also note than I'm an emacs user, and so this may be an emacs specific question. I've updated the tags to reflect this.

+8  A: 

"indent" and "cb" come with most distros I've had...

nitzmahone
+7  A: 

Did you look at GNU indent ?

Dirk Eddelbuettel
+5  A: 

Astyle is worth a look.

Duck
+1  A: 

emacs? That's probably more than you're looking for, but it WILL do it...

Brian Postow
and Vim, of course.
Jefromi
How would one do that? I used GNU Indent as @Dirk Eddelbuettel and others suggested, but now when I edit using Emacs, it keeps wanting to set the indentation back to the default, instead of the 8 I made it when I used Indent, and it's getting kind of annoying. It looks like for an emacs user, it doesn't really make sense to use anything other than emacs for formatting..
Ralph
you want to set your c-style eventually, in your .emacs you will want the line (c-set-style "______") in your c-mode hook.To chose the style, I'd play with them with just calling c-set-style manually (M-x ....) hitting tab gives the whole list. but if you want 8 char tabs, your options are bsd, linux, and python. The names have nothing to do with what you're using them for, just where the style came from. After you set the style, I'd selelect the document and then do M-x indent region to test.
Brian Postow
You can also *completely* customize the indentation style if you really want...
dmckee
dmckee: Yes, in act I have done that myself... but that's probably more than OP wants to worry baout... (In case not, here's my definition: (c-add-style "acordex" '((c-basic-offset . 4) (c-comment-only-line-offset . 0) (c-hanging-braces-alist (substatement-open before after)) (c-offsets-alist (topmost-intro . 0) (substatement . +) (substatement-open . 0) (case-label . +) (access-label . -) (inclass . ++) (inline-open . 0))) ))
Brian Postow
+2  A: 

If you want the (external) indentation engine to play well with Emacs, you'll want to be sure to set up your cc-mode indentation to match. There are many built-in styles you can use. And if none of them meet your needs, you can customize the settings (of particular use is the interactive customization).

Another, inferior, solution would be to set up your before-save-hook to call the indentation scheme. Something like the following:

(add-hook 'c-mode-common-hook 
     (labmda ()) (add-hook (make-local-variable 'before-save-hook) 
                           'indent-it-all))
(defun indent-it-all ()
  "indent the buffer using indent"
  (shell-command-on-region (point-min) (point-max) "indent" t t))
Trey Jackson
A: 

This is what I use personally, and I think it satisfies all of your indentation needs, you can try it by adding the code to your .emacs file. This function takes care of proper indentation when I'm writing 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 wish to re-indent 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 a "programming" file in emacs. That way, I always see well-indented code. I can put up that bit of code too I you want it.

vedang