Formatting C++ files

7 views
Skip to first unread message

Totte Karlsson

unread,
Oct 28, 2011, 9:35:06 PM10/28/11
to vim...@googlegroups.com
Hi,
I am going trough many C++ headers and need to do some formatting.

The first I need to do is related to C++ headers:

a lot of them look has lines like this

<some white space>void<some white space>AFunction();

where the <some whitespace> is just some whitespace.

that I need to convert to a more fixed and defined format:
<two tabs>void<8 tabs>AFunction(); or

<8 spaces>void<32 spaces>AFuntion();

I wonder how do I create a substitute expression for the above?

cheers,
Totte


Taylor Hedberg

unread,
Oct 29, 2011, 4:06:35 AM10/29/11
to vim...@googlegroups.com
I assume that not every function in the headers is called "AFunction"
with void return type, so this is a little more liberal than precisely
what you stated. It will apply to any line that has some whitespace,
followed by one or more words (the return type, including '*' for
pointer types), followed by a function name and its argument list.

:%s/\v^\s+([a-zA-Z_* ]+)\s+(\*?)(\s*\w+\(.*\);)$/\t\t\1\2\t\t\t\t\t\t\t\t\3/

It's perhaps a little more complex than it needs to be, because it
handles cases where you might have something like this:

int *my_function();

where the '*' is considered part of the return type, not the function
name, and you would presumably want it separated from the function name
when you convert the whitespace.

There may be corner cases in your header files that I didn't think of,
so if this doesn't apply to any of the lines that you need converted,
then you may need to tweak it. Or you can post the problematic lines
here and we can give you additional help.

Totte Karlsson

unread,
Oct 29, 2011, 5:26:45 PM10/29/11
to vim...@googlegroups.com
On 10/29/2011 1:06 AM, Taylor Hedberg wrote:
> I assume that not every function in the headers is called "AFunction"
> with void return type, so this is a little more liberal than precisely
> what you stated. It will apply to any line that has some whitespace,
> followed by one or more words (the return type, including '*' for
> pointer types), followed by a function name and its argument list.
>
> :%s/\v^\s+([a-zA-Z_* ]+)\s+(\*?)(\s*\w+\(.*\);)$/\t\t\1\2\t\t\t\t\t\t\t\t\3/
>
That is great! It "kind" of works.
One question. Would it be possible instead of all the tabs, position the "last"
part on say position 49 on the line? This because, sometimes the first token(s)
is really long, causing something like the following:

mtkVirtualHole* GetVirtualBond(const int& i);
void DeleteVirtualBond();

where desired result is
mtkVirtualHole* GetVirtualBond(const int& i);
void DeleteVirtualBond();
(used fewer tabs here for simplicity)


-totte


Taylor Hedberg

unread,
Oct 29, 2011, 8:22:08 PM10/29/11
to vim...@googlegroups.com
Totte Karlsson, Sat 2011-10-29 @ 14:26:45-0700:

> That is great! It "kind" of works. One question. Would it be possible
> instead of all the tabs, position the "last" part on say position 49
> on the line? This because, sometimes the first token(s) is really
> long, causing something like the following:

Sure, anything is possible if you write a complicated enough expression!
:)

:%s/\v^\s+([a-zA-Z_* %]+)\s+(\*?)(\s*\w+\(.*\);)$/\="\t\t".submatch(1).submatch(2).repeat(' ',49-(2*&ts+len(submatch(1))+len(submatch(2)))).submatch(3)

This puts 2 tabs at the beginning of the line, followed by the return
type, then pads with spaces out to column 49, then finishes with the
function name and argument list.

If you plan on reusing this command with any regularity, it would
probably make sense to define a custom command so you can easily repeat
it. Because let's face it, the above is not something you really want to
type more than once!

If you add the following lines to your .vimrc, you can then just use
`:FormatPrototypes` to perform this operation in the current buffer:

command! FormatPrototypes
\ %substitute/\v^\s+([a-zA-Z_* ]+)\s+(\*?)(\s*\w+\(.*\);)$/\=
\ "\t\t" . submatch(1) . submatch(2) .
\ repeat(' ', 49 - (2 * &tabstop + len(submatch(1)) + len(submatch(2)))) .
\ submatch(3)

Reply all
Reply to author
Forward
0 new messages