Find multiple occurrences of modified code

Help with writing and playing macros

Find multiple occurrences of modified code

Postby danielbureau » Wed Jul 26, 2006 11:39 am

Hello all:

I use a product that generates Java code. Whenever I need to make a change to the generated code, I surround my modified lines with comments, similar to:
//START-MOD: .....
code
code
code
//END-MOD:

Note that a single file can have multiple modifications. I need to identify all occurrences of modified code and list their content.

I have tried this regular expression, created by Mofi in another post:
START-MOD:[~^[]+END-MOD

It does not list the blocks indivudually. Instead it find the first START-MOD then the last END-MOD and list everything in between.

Can someone help me.
Thank in advance, your help greatly appreciated
Daniel Bureau
User avatar
danielbureau
Newbie
 
Posts: 3
Joined: Mon Jul 24, 2006 11:00 pm

Re: Find multiple occurrences of modified code

Postby Mofi » Thu Jul 27, 2006 5:58 am

A single regular expression is not possible here. You have to use a macro with the macro property Continue if a Find with Replace not found enabled. The following macro is without any regular expression search.

InsertMode
ColumnModeOff
HexOff
Bottom
IfColNum 1
Else
"
"
EndIf
Top
Clipboard 9
ClearClipboard
Loop
Find "//START-MOD:"
IfNotFound
ExitLoop
EndIf
EndSelect
Key HOME
StartSelect
Find Select "//END-MOD:"
Key HOME
Key DOWN ARROW
CopyAppend
EndSelect
EndLoop
NewFile
Paste
ClearClipboard
Clipboard 0
Top
Key END
IfColNum 1
"No modified code block found !!!"
Else
Key HOME
EndIf

First the macro terminates the last line with CRLF, if this is not already done. This is necessary in the case where the last line is a //END-MOD: line because the Key DOWN ARROW would not work if the last line is not terminated with CRLF in this special case.

Next it clears clipboard 9 which is used as buffer for the found modified code blocks.

Inside the loop the first find searches for the start of a modified code block and moves the cursor to start of this line if found. If the start is not found anymore, the loop exits. Next the macro selects everything from current cursor position to end of the modified code block and start of the next line.
This selection is appended to the existing content in clipboard 9.

After the loop, the content in clipboard 9 is pasted into a new file. If the file is empty because no modified block was found, it writes an appropriate message into the new file.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Find multiple occurrences of modified code

Postby danielbureau » Thu Jul 27, 2006 6:45 pm

Thank you Mofi for the quick reply.

I have been doing some digging in documentation and other forum subjects because I want to further extend the macro.

Like many others, I want to apply the search/macro to many files, but not all files are under the same folder. There are many folders containing java files for a given project. So the "DOS start .... " does not seam to work, I did not find a switch that will open files in sub-directories.

I would also like to create a report similar to the ** Find Results ** by appending file name. I have tried inserting "--------------" as a line separator but it goes into the original file instead of the clipboard.

At line 18, you have an EndSelect without a StartSelect. Do you have a reason for it or is it just an oversight?

Daniel Bureau
Thank you from Canada
User avatar
danielbureau
Newbie
 
Posts: 3
Joined: Mon Jul 24, 2006 11:00 pm

Re: Find multiple occurrences of modified code

Postby Mofi » Fri Jul 28, 2006 6:21 am

I have posted some macro solutions to run a macro on all open files. The first posted macro is the submacro named for example "CollectMods". You have to remove the commands Clipboard 9 and ClearClipboard from CollectMods macro and also cut the lines after EndLoop into the new macro. So the submacro CollectMods looks like this:

Bottom
IfColNum 1
Else
"
"
EndIf
Top
Loop
Find "//START-MOD:"
IfNotFound
ExitLoop
EndIf
EndSelect
Key HOME
StartSelect
Find Select "//END-MOD:"
Key HOME
Key DOWN ARROW
CopyAppend
EndSelect
EndLoop


The main macro runs on all open NAMED files and closes all these files without saving. So first make sure all files are saved and you don't have new files opened before running the macro. The macro also needs the property Continue if a Find with Replace not found checked.

InsertMode
ColumnModeOff
HexOff
Clipboard 9
ClearClipboard
Loop
IfNameIs ""
ExitLoop
EndIf
Top
Find "//START-MOD:"
IfFound
Clipboard 8
CopyFilePath
Top
"

File: "
Paste
"

"
SelectToTop
Clipboard 9
CopyAppend
EndSelect
PlayMacro 1 "CollectMods"
EndIf
CloseFile NoSave
EndLoop
NewFile
Paste
ClearClipboard
Clipboard 8
ClearClipboard
Clipboard 0
Top
Find "//START-MOD:"
IfNotFound
SelectAll
"No modified code block found in all open files !!!"
Else
Top
Key DEL
Key DEL
EndIf


Additionally you have to open all files which contain the string "//START-MOD:" and no other files before running the macro above. This can be easily done by searching in your directory structure for files *.* which contain the string with your file manager (Windows Explorer, Total Commander, ...), select the files found and drag & drop it to the UltraEdit window where no other file is open.

It's also possible to simply open all files including subdirectories with UltraEdit with the Quick Open command and run the macros on all opened files independent if it contains the string or not. This would be slower because more files must be searched for a modified block, but would work too because the main macro first searches for the string and runs the submacro only if the current file contains the string.


I have inserted the EndSelect at line 18 of the first macro because the Find above automatically creates a selection. Most of the time it is not needed but I have had some bad effects after a find with following cursor moves if the selection is not ended. So I do it now always for security.

Hope all this works, because I have not tested it.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Find multiple occurrences of modified code

Postby danielbureau » Fri Jul 28, 2006 11:38 am

Thank you very much. I will look into that.
Have a great weekend.
Daniel Bureau
User avatar
danielbureau
Newbie
 
Posts: 3
Joined: Mon Jul 24, 2006 11:00 pm


Return to Macros