Fwd: Changing C comments

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Fwd: Changing C comments

Bob von Knobloch
Hello,

I have to update many c-source programs and one important point is to
change all the end-of-line comments from 'old C' (/*--*/) to C++ type
(//---). This seems (to me) to be ajob that is often required but I am
unable to find anything on this topic on the web. (Google is, seemingly,
not my friend).
Ideally, Iwould like to change the comments (only single-line ones
though so /* and */ appearing on a line without their 'friend' should
remain.
It would also save a lot of work if there were a way to format all the
new comments so that the '//' was set at a given column (or after the
code if it's too long).
Can anyone point me to some scripts or whatever that might help ??
Sorry if this stuff is easy to find, I can't.

Best regards,

Bob von Knobloch.
--
The Sun is out, the sky is blue. It's time to drive the MR2.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Changing C comments

Benjamin Fritz
On Friday, September 28, 2012 6:34:24 AM UTC-5, Bob von Knobloch wrote:

> Hello,
>
>
>
> I have to update many c-source programs and one important point is to
>
> change all the end-of-line comments from 'old C' (/*--*/) to C++ type
>
> (//---). This seems (to me) to be ajob that is often required but I am
>
> unable to find anything on this topic on the web. (Google is, seemingly,
>
> not my friend).
>
> Ideally, Iwould like to change the comments (only single-line ones
>
> though so /* and */ appearing on a line without their 'friend' should
>
> remain.
>
> It would also save a lot of work if there were a way to format all the
>
> new comments so that the '//' was set at a given column (or after the
>
> code if it's too long).
>
> Can anyone point me to some scripts or whatever that might help ??
>
> Sorry if this stuff is easy to find, I can't.
>

A built-in solution would be a regular expression substitute:

%s#\v/\*(%(%(\*/)@!.)*)\*/\s*$#//\1

\v - turn on "very magic"
/\* match beginning of C-style comment
(...) capture the text inside the comment as follows:
  %(...)* group but don't capture, matching the following any number of times:
    %(\*/)@! don't match the end of a C-style comment "*/"
    . but match any other character at all
\*/ match the end of the C-style comment
\s*$ make sure the C-style comment is the last thing on the line

The replace uses //\1, which is "//" followed by the captured text (the comment content). Note this will also remove any trailing whitespace on the line.

I've used '#' instead of the traditional '/' as the pattern delimiter since '/' characters are part of the pattern and replacement text.

As requested, this works on single lines only. It might be possible to tweak it to work on multiple lines but that would probably be easier with a different method.

References:
:help /\v
:help /\@!
:help /\(
:help /\%(
...and other parts of :help pattern.txt.

If this is too complicated (regular expressions are a powerful tool but can be hard to use; "now you have two problems" and all that) then you can try out a commenting plugin. I've heard good things about "NERD Commenter", which appears to have a "switch comment style" command:

  http://www.vim.org/scripts/script.php?script_id=1218

For some reason I cannot currently find any others which seem regularly maintained. I thought I remembered seeing a wide variety of them at some point.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Changing C comments

Bob von Knobloch
On 28/09/12 20:39, Ben Fritz wrote:
<snip>
> If this is too complicated (regular expressions are a powerful tool but can be hard to use; "now you have two problems" and all that) then you can try out a commenting plugin. I've heard good things about "NERD Commenter", which appears to have a "switch comment style" command:
>
>    http://www.vim.org/scripts/script.php?script_id=1218
>
> For some reason I cannot currently find any others which seem regularly maintained. I thought I remembered seeing a wide variety of them at some point.
>

Ben,
your regex works like a charm.
It replaces all occurrences in the file, not just 1 line, as you thought
and it leaves 1 space at the end of each converted comment (not a
problem to globally remove).
Now I need to align them. The NERD Commenter didn't work well for me.
Pity, it looked as though it could solve my aligning problem.
Now I need a K&R style to "braces horizontally aligned" brace conversion :=)

Many thanks,

Bob

--
The Sun is out, the sky is blue, it's time to drive the MR2.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Changing C comments

Benjamin Fritz
On Friday, September 28, 2012 3:01:03 PM UTC-5, Bob von Knobloch wrote:

> On 28/09/12 20:39, Ben Fritz wrote:
>
> <snip>
>
> > If this is too complicated (regular expressions are a powerful tool but can be hard to use; "now you have two problems" and all that) then you can try out a commenting plugin. I've heard good things about "NERD Commenter", which appears to have a "switch comment style" command:
>
> >
>
> >    http://www.vim.org/scripts/script.php?script_id=1218
>
> >
>
> > For some reason I cannot currently find any others which seem regularly maintained. I thought I remembered seeing a wide variety of them at some point.
>
> >
>
>
>
> Ben,
>
> your regex works like a charm.
>
> It replaces all occurrences in the file, not just 1 line, as you thought
>

I meant, it will work for this:

/* this is a single-line C-style comment */

But not this:

/* this is
 * a multi-line
 * C-style comment
 */

> and it leaves 1 space at the end of each converted comment (not a
> problem to globally remove).
>

Oops. Here's a tweak to not include the trailing whitespace within the comment:

%s#\v/\*(%(%(\*/)@!.){-})\s*\*/\s*$#//\1

(untested)

As you can see, you can easily spend WAY to long tweaking these.

Here I'm using a "non-greedy" regular expression, see :help /\{-

> Now I need to align them. The NERD Commenter didn't work well for me.
>
> Pity, it looked as though it could solve my aligning problem.
>
> Now I need a K&R style to "braces horizontally aligned" brace conversion :=)
>

I'd understand your alignment problem better if you gave an example. But probably it's not something you should use regex to accomplish. There is an "Align" plugin by Dr. Chip but since I don't really understand your problem I'm not sure if it's useful:

  http://vim.sourceforge.net/scripts/script.php?script_id=294

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Changing C comments

Marc Jessome
In reply to this post by Bob von Knobloch
Bob,

Perhaps something along the lines of http://astyle.sourceforge.net/ 
would be what you are looking for. Simple regexes might not be what
you're looking for here.

Marc

On 28/09/12 04:00 PM, Bob von Knobloch wrote:

> On 28/09/12 20:39, Ben Fritz wrote:
> <snip>
>> If this is too complicated (regular expressions are a powerful tool
>> but can be hard to use; "now you have two problems" and all that) then
>> you can try out a commenting plugin. I've heard good things about
>> "NERD Commenter", which appears to have a "switch comment style" command:
>>
>> http://www.vim.org/scripts/script.php?script_id=1218
>>
>> For some reason I cannot currently find any others which seem
>> regularly maintained. I thought I remembered seeing a wide variety of
>> them at some point.
>>
>
> Ben,
> your regex works like a charm.
> It replaces all occurrences in the file, not just 1 line, as you thought
> and it leaves 1 space at the end of each converted comment (not a
> problem to globally remove).
> Now I need to align them. The NERD Commenter didn't work well for me.
> Pity, it looked as though it could solve my aligning problem.
> Now I need a K&R style to "braces horizontally aligned" brace conversion
> :=)
>
> Many thanks,
>
> Bob
>

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Changing C comments

jreidthompson-2
In reply to this post by Bob von Knobloch
On Friday, September 28, 2012 7:34:24 AM UTC-4, Bob von Knobloch wrote:
> Hello,
>
>
>
> I have to update many c-source programs and one important point is to
>
> change all the end-of-line comments from 'old C' (/*--*/) to C++ type
>
> (//---). This seems (to me) to be ajob that is often required but I am

are you certain this code will never need to be compiled on an "older" compiler?  ISO C90 or earlier, any C compiler that will not allow C++ style comments, etc???

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Changing C comments

Bob von Knobloch
In reply to this post by Benjamin Fritz
On 28/09/12 22:36, Ben Fritz wrote:
<snip>

> Oops. Here's a tweak to not include the trailing whitespace within the comment:
>
> %s#\v/\*(%(%(\*/)@!.){-})\s*\*/\s*$#//\1
>
> (untested)
>
> As you can see, you can easily spend WAY to long tweaking these.
>
> Here I'm using a "non-greedy" regular expression, see :help /\{-
>
>> Now I need to align them. The NERD Commenter didn't work well for me.
>>
>> Pity, it looked as though it could solve my aligning problem.
>>
>> Now I need a K&R style to "braces horizontally aligned" brace conversion :=)
>>
>
> I'd understand your alignment problem better if you gave an example. But probably it's not something you should use regex to accomplish. There is an "Align" plugin by Dr. Chip but since I don't really understand your problem I'm not sure if it's useful:
>
>    http://vim.sourceforge.net/scripts/script.php?script_id=294
>
You are right Ben,
For braces, I wanted to change e.g. :

func() {
body; }

to:

func()
{
     body;
}

Marc Jessome suggested "http://astyle.sourceforge.net/" for this job and
it works like a charm for all possible brace combinations. It's not a
tool I knew of.

Thanks Marc.

Now for comments, I want to change:

      // Comment1
                      // Comment2

to:

                              // Comment1
                              // Comment2
where I can specify ---------^ this column.
(Long source code lines wuill 'push' this to the right)

(Format might get mangled by mail/list) :=)

On 29/09/12 02:27, jreidthompson wrote:
 > are you certain this code will never need to be compiled on an
"older" compiler?  ISO C90 or earlier, any C compiler that will not
allow C++ style comments, etc???

Yes thanks, that is exactly why I want to convert the files.
I find the older styles harder to ascertain the logical flow in. (Just
my preference).

Many thanks to all,

Bob

--
The Sun is out, the sky is blue, it's time to drive the MR2.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php