macro to comment block of code

Help with writing and playing macros

macro to comment block of code

Postby mathmax » Wed Jun 13, 2007 1:50 pm

Hello,

I would like to have a macro that comment several lines of code like that.

Before:
Code: Select all
if(...)
  ...
endif
...

After:
Code: Select all
//  ...
//endif
//if(...)
//...

How to do it?

Thank you in advance for your help.
User avatar
mathmax
Basic User
Basic User
 
Posts: 10
Joined: Thu May 24, 2007 11:00 pm

Re: macro to comment block of code

Postby Mofi » Wed Jun 13, 2007 2:12 pm

No need for a macro, click on Edit - Comment Add after selecting the block. If it is greyed out, your syntax highlighting language for your current file does not have a Line Comment = entry.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: macro to comment block of code

Postby mathmax » Wed Jun 13, 2007 4:13 pm

Ok, but I think I have to write a macro for adding a block comment; I write this:

InsertMode
ColumnModeOff
HexOff
PerlReOn
Copy
"/*"
Paste
"*/"

But I would rather prefer to avoid to copy the selected text in the clipboard. Is there any other way to do that?
I would also uncomment the text if no text is selected and the mouse cursor is between /* and */ with the same macro. How can I do that?
User avatar
mathmax
Basic User
Basic User
 
Posts: 10
Joined: Thu May 24, 2007 11:00 pm

Re: macro to comment block of code

Postby Mofi » Thu Jun 14, 2007 6:35 am

Add the block comment with a template as I have described at CSS add/remove comment button greyed out.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: macro to comment block of code

Postby mathmax » Thu Jun 14, 2007 1:02 pm

Thank you. And how to uncomment a text when the mouse cursor is situated between /* and */?
User avatar
mathmax
Basic User
Basic User
 
Posts: 10
Joined: Thu May 24, 2007 11:00 pm

Re: macro to comment block of code

Postby Mofi » Mon Oct 15, 2007 1:03 pm

Here is a macro which works only on a selection without a template and automatically detects if the selected block is already a commented block (contains /* anywhere) or not.

It is not possible to restrict the search for /* to only start of the selected text. But you can use a regular expression search for "/*" with additional regular expression characters if you want to restrict it a little bit.

Disadvantage of this solution: It works only for blocks up to 30,000 bytes.

IfSel
Find "/*"
Replace All SelectText ""
IfFound
Find "*/"
Replace All SelectText ""
Else
Find "^s"
Replace "/*^s*/"
EndIf
EndIf

Here is a different solution using a user clipboard and therefore practically no length limit.

IfSel
Find "/*"
Replace All SelectText ""
IfFound
Find "*/"
Replace All SelectText ""
Else
Clipboard 9
Cut
"/*"
Paste
"*/"
ClearClipboard
Clipboard 0
EndIf
EndIf
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: macro to comment block of code

Postby mathmax » Mon Oct 15, 2007 8:11 pm

Ok, I've just installed UES 6.40 and it works! I'm so lucky.
User avatar
mathmax
Basic User
Basic User
 
Posts: 10
Joined: Thu May 24, 2007 11:00 pm


Return to Macros