Comment a block of code using ' single quotes

Help with writing and playing macros

Comment a block of code using ' single quotes

Postby matthewk » Fri Aug 03, 2007 6:14 pm

Hello,
Will someone guide me on making a macro to comment a block of code? Given a selection of a block of n number of lines, I would like to place a single quote at the beginning of each line. And, likewise, a macro to remove the single quote from the beginning of a selection of n lines.

Ex. Need to use macro A:
if i = 1 then
response.write (i)
end if

After using macro A:
'if i = 1 then
' response.write (i)
'end if

Then after using macro B, I want it to revert back:
if i = 1 then
response.write (i)
end if

Thanks for any guidance or examples that might help me on this.
User avatar
matthewk
Newbie
 
Posts: 7
Joined: Thu Aug 02, 2007 11:00 pm

Re: Comment a block of code using ' single quotes

Postby mrainey56 » Fri Aug 03, 2007 7:52 pm

At its most basic:

Comment Selection

InsertMode
ColumnModeOff
HexOff
UnixReOn
Find RegExp "^"
Replace All SelectText "'"



Uncomment Selection

InsertMode
ColumnModeOff
HexOff
UnixReOn
Find RegExp "^'"
Replace All SelectText ""
User avatar
mrainey56
Master
Master
 
Posts: 212
Joined: Tue Jul 27, 2004 11:00 pm
Location: Spartanburg, South Carolina

Re: Comment a block of code using ' single quotes

Postby matthewk » Fri Aug 03, 2007 9:09 pm

That works great, thanks :)

The only thing is that the line where the selection begins does not get commented/uncommented with the macro unless the selection began at the very beginning of a line.
Ex:
te[begin selection]xt text text text text text
text text text text te[end selection]xt text

Result: using comment macro
text text text text text text
'text text text text text text

However Ex:
[begin selection]text text text text text text
text text text text[end selection] text text


Result: using comment macro (desired result)
'text text text text text text
'text text text text text text

And, for uncommenting, all of the 's need to be included in the selection.
Ex.
't[begin selection]ext text text text text text
'[end selection]text text text text text text

Result: using uncomment macro
'text text text text text text
text text text text text text

Ex. (similar)
[begin selection]'text text text text text text
[end selection]'text text text text text text

Result: using uncomment macro
text text text text text text
'text text text text text text

Ex. (showing what's required for desired result)
[begin selection]'text text text text text text
'[end selection]text text text text text text

Result: using uncomment macro
text text text text text text
text text text text text text

Thanks again, works great if selecting appropriately.
User avatar
matthewk
Newbie
 
Posts: 7
Joined: Thu Aug 02, 2007 11:00 pm

Re: Comment a block of code using ' single quotes

Postby Mofi » Sat Aug 04, 2007 2:11 pm

Have you a syntax highlighting definition for your file?

Why? Because if you have a syntax highlighting definition for your file and it contains at the language definition line Noquote Line Comment = ' which means your language does not have strings or Line Comment = ' String Chars = " which means your language uses only double quotes for strings, then you can use Edit - Comment Add and Edit - Comment Remove.

That would also insert/remove the single quote of the selected block even when the block selection does not start at the beginning of a line.

By the way: The first macro simply searches in the selected area for the start of a line and inserts a single quote. The second macro searches in the selected area for a single quote at start of a line and deletes it. So it is not really surprising that it works only perfect for selected areas with only full lines selected. The macros use the Unix regular expression engine. You should know this for future usage of regular expression finds and replaces.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4062
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Comment a block of code using ' single quotes

Postby matthewk » Sun Aug 12, 2007 12:19 am

Thanks a lot to you both. I didn't know there was a line comment tool already built-in. I mapped the line tool insert/delete to ctrl+ins and ctrl+del, which is great :)

The other thing I have came into is that I need a quick way to comment and uncomment html and css. I know UE can handle syntax highlighting very well, so I'm wondering can it also do the comment adding and deleting based on the language that it realizes I am coding in? I would really like to use ctrl+ins and ctrl+del to insert and remove comments no matter which language I am in. I saw the example of a file extension check, but I don't think that would work since, for example in an asp page, I can do multiple languages in the same file.
html: viewtopic.php?t=4358
css: viewtopic.php?t=4344

Thanks again.
User avatar
matthewk
Newbie
 
Posts: 7
Joined: Thu Aug 02, 2007 11:00 pm

Re: Comment a block of code using ' single quotes

Postby Mofi » Sun Aug 12, 2007 10:14 am

As described in the 2 topics you linked to, Comment Add and Comment Remove works only with line comments. But HTML and CSS don't have line comments. So what you like is not possible.

You must write macros for those languages which do not have a line comment. You can combine all macros for "Insert Block Cmt" and all macros for "Remove Block Cmt" by using the IfExtIs command. But you will always need 4 keys - 2 for "Line Comment Add/Remove" (built-in) and 2 for "Insert/Remove Block Cmt" macros. For multi-language files like ASP you must take care where you press which hotkey.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4062
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Macros