[lug] Source Code Pretty-fying

Tkil tkil at scrye.com
Tue Sep 14 18:44:57 MDT 2004


>>>>> "Matt" == Matt Thompson <thompsma at colorado.edu> writes:

Matt> I have some C code that has been hacked on by about 4-5 people.
Matt> Apparently, each person had their own idea on what "tab" should
Matt> do.  I say 3 spaces, another said 2 spaces, another said the
Matt> hard \t.  So, when I open this code in VIM, I get code that is
Matt> wacky-indented.  You know, some lines inside like 3 if's are
Matt> underindented, etc.

The usual culprit is people used to windows systems, which have
adopted a 4-spaces-per-tab-character standard.  You can set tab widths
in vi, but I don't recall exactly how.

As for imposing style in vi for new code, I don't know about that
either.  (x)emacs has an editing mode that will automatically build
brace blocks with customizable indentation and newline arrangements,
and configures the tab key to go to an appropriate indentation level
(with either tabs, spaces, or a mix); I suspect there are similar
add-ins for vi.

Seems like a place to start might be:

   http://hermitte.free.fr/vim/c.php

Some other comments on using modes in VIM:

   http://www.justlinux.com/nhf/Programming/Introduction_to_C_Programming.html

Matt> Thus, I figured there has to be some utility that will resolve
Matt> this and impose regular indenting.  Heck, there might be
Matt> something for VIM, but I couldn't see it.  Any help?

As Ralf already indicated, you probably have 'indent' installed on
your system already, and it can do much of this.

As he also said, this incantation will have (x)emacs indent the
current file:

   C-x h M-x untabify RET
   C-x h M-x indent-region RET

("Untabify" is the in-emacs equivalent of 'expand' at the command
prompt...)

The magic phrase you are looking for, though, to have google tell you
all about these lovely toys, is "pretty printer":

  http://www.google.com/search?q=source+code+pretty+printer

Happy hacking,
t.



More information about the LUG mailing list