Toggle Cobol Comment in Selected Text

Help with writing and playing macros

Toggle Cobol Comment in Selected Text

Postby weirich_j » Sat Jul 07, 2007 7:29 pm

I'm looking for a macro that will toggle a comment in cobol code for every line of selected text. Comments in cobol are indicated with a star in column 7.

For example, if the following three lines were highlighted:

Code: Select all
      * commented before
         not commented before
         some random text


Upon excecuting the macro, the text would change to:

Code: Select all
         commented before
      * not commented before
      * some random text


Thanks in advance.
User avatar
weirich_j
Newbie
 
Posts: 3
Joined: Fri Jul 06, 2007 11:00 pm

Re: Toggle Cobol Comment in Selected Text

Postby Mofi » Sun Jul 08, 2007 4:40 pm

Before I can give you the simple macro which does the job I need the information how the lines are indented: with spaces or with a tab?

And should the inserted asterisk replace the space/tab at column 7 or should it be inserted?

And should the existing asterisk at column 7 of comment lines replaced by a space/tab or should it only be deleted?

I can't see with your example which rule should be used for the indentation.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Toggle Cobol Comment in Selected Text

Postby weirich_j » Sun Jul 08, 2007 5:23 pm

Mofi wrote:Before I can give you the simple macro which does the job I need the information how the lines are indented: with spaces or with a tab?


With spaces. If it wouldn't add too much overhead, it might be nice at the beginning for all tabs to be converted to spaces, in case a tab might inadvertently have been left in there. Some times I work on code done by other programmers, so I can't really guarantee there won't be a tab.


Mofi wrote:And should the inserted asterisk replace the space/tab at column 7 or should it be inserted?


Please have it replace the space in column 7 with the asterisk, not insert. Although an insert would achieve the same result, the replace looks much cleaner.

Mofi wrote:And should the existing asterisk at column 7 of comment lines replaced by a space/tab or should it only be deleted?


The asterisk in column 7 should be replaced with a single space.

Thanks Again!
User avatar
weirich_j
Newbie
 
Posts: 3
Joined: Fri Jul 06, 2007 11:00 pm

Re: Toggle Cobol Comment in Selected Text

Postby Mofi » Sun Jul 08, 2007 5:44 pm

Ok, here is the macro which should work as you requested:

Code: Select all
IfSel
InsertMode
ColumnModeOff
HexOff
UnixReOff
Find RegExp "%^t"
Replace All SelectText "^t"
IfFound
TabsToSpaces
EndIf
Find RegExp "%      ^*"
Replace All SelectText "CoMmEnT"
Find RegExp "%       "
Replace All SelectText "      *"
Find MatchCase RegExp "%CoMmEnT"
Replace All SelectText "       "
EndIf


Add UnixReOn or PerlReOn (v12+ of UE) at the end of the macro before command EndIf if you do not use UltraEdit style regular expressions by default - see search configuration. Macro command UnixReOff sets the regular expression option to UltraEdit style.

How the macro works:

First it checks if currently a selection exists and if not, it does nothing (security).

Next it checks if there is a tab at start of any selected line. If this is true, all tabs in the current selection are converted to spaces using the current tab stop value(s) configured for this file. This command breaks the Undo chain. You must consider this!!!

The rest are very simple UltraEdit style regular expressions. First all existing comment lines are temporarily marked with a special string. Then all other lines are converted to comment lines. And last the previous special marked comment lines are converted to normal code lines.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Toggle Cobol Comment in Selected Text

Postby weirich_j » Sun Jul 08, 2007 7:21 pm

Thank you for the fast response, works fine. Sometimes trailing spaces are found at the end of a line of code as well. I guess the 'trim trailing spaces' could be utilized in the same manner as the 'TabsToSpaces', but we would still have the problem of the patterns existing in a string.
User avatar
weirich_j
Newbie
 
Posts: 3
Joined: Fri Jul 06, 2007 11:00 pm


Return to Macros

cron