Well, while I agree with rhapdog in general. But there are cases where it makes sense to use UltraEdit as preprocessor using macros or scripts.
In our company there is such a case. There is a file format which can be only edited with a text editor like UltraEdit. Files with this format are interpreted by an application which produces a binary file from the ASCII file. For some reasons I do not want to explain here it makes sense in once case to have the information of several of those ASCII files in a single file for development while in other cases there are always separate files.
On releasing a new version, the version string must be edited in this "common" file with UltraEdit and then UltraEdit macros are used to create copies of this common file with different file names containing different blocks from the common file depending on output file and also some other modifications are done depending on output file.
In this case it was much easier to create a set of UltraEdit macros and a master macro to produce the other ASCII files from the common ASCII file then coding this in the application which interprets the ASCII files and produces the binary files. It is also much easier to handle the ASCII files with the UltraEdit macro set. And we have to archive the produced ASCII files while the common ASCII file must not be archived. Rewriting the application which reads in the ASCII files and outputs the binary files to make additionally this preprocessing and produce additionally also the ASCII files to archive would be definitely much more difficult to code and handle.
To
taskin:
It is surely no problem to code a macro or perhaps better a script to preprocess the file. Here is a script example for your requirements according to what you have written. On execution from script list it asks you which blocks should become active according to a keyword. For more details read the comments of the script.
- Code: Select all
if (UltraEdit.document.length > 0) { // Is any file currently opened?
var sDefine = UltraEdit.getString("Enter the preprocessor definition keyword:",1);
if (sDefine.length) {
UltraEdit.insertMode(); // Define environment for the script.
UltraEdit.columnModeOff();
UltraEdit.activeDocument.hexOff();
UltraEdit.activeDocument.top();
// Define the parameters for a Perl regular expression search to
// find lines between a line with //#IFDEF and a line with //#ENDIF.
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.matchCase=true;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=true;
UltraEdit.activeDocument.findReplace.searchDown=true;
if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean") {
UltraEdit.activeDocument.findReplace.searchInColumn=false;
}
UltraEdit.perlReOn();
// Run a case sensitive Perl regular expression Find to find and select
// blocks with lines between a line starting with //#IFDEF and a line
// starting with //#ENDIF.
while (UltraEdit.activeDocument.findReplace.find("//#IFDEF.*\\r\\n(?:.*\\r\\n)+//#ENDIF")) {
// Get the selected block into an array of strings
var sBlockLines = UltraEdit.activeDocument.selection.split("\r\n");
var bModified = false;
// Check if entered keyword is present in first line of the block.
if (sBlockLines[0].indexOf(sDefine) < 0) {
// The keyword is not present in first line with //#IFDEF.
// Comment all lines of this block if not already commented.
for (var nLine = 1; nLine < (sBlockLines.length-1); nLine++) {
// Starts the line already with // ?
if (sBlockLines[nLine].indexOf("//") != 0) {
sBlockLines[nLine] = "//" + sBlockLines[nLine];
bModified = true;
}
}
} else {
// The keyword is present in first line with //#IFDEF.
// Uncomment all lines of this block if currently commented.
for (var nLine = 1; nLine < (sBlockLines.length-1); nLine++) {
// Starts the line already with // ?
if (sBlockLines[nLine].indexOf("//") == 0) {
sBlockLines[nLine] = sBlockLines[nLine].substr(2);
bModified = true;
}
}
}
// If no modification of this block is necessary, just cancel the
// current selection. Otherwise join the lines in the string array
// back to a block string and write the modified block over the
// still selected block in active file.
if (!bModified) UltraEdit.activeDocument.cancelSelect();
else UltraEdit.activeDocument.write(sBlockLines.join("\r\n"));
}
UltraEdit.activeDocument.top();
}
}