Nicely done. (and well-programmed)
I like more whitespace than you do.
I expanded the script and added a Count function.
I had to kind of guess at the German translation for the new strings.
It also does RegEx searching. (fixed the code)
I also tried to make it work with RegEx and I can't figure out how to do it.
The function RegExPos (sSearchtext, sLineText, &sExpr);
requires that the last item be a pointer to a string for
the resultant string found.
But afaik, except for objects, JavaScript only passes by value, not reference.
Unless FastScript supports something else, but I could find no info or documentation or examples.
With: var sExpr; // just defined and not set to a blank string value
With Count checked and Contain clicked, if I pass the string variable itself I get the error:
Could not convert variant of type (Null) into type (OleStr).
With: var sExpr = "";
It executes, but chooses all of the lines to delete.
(for a supposed RegEx of "[0-9]+" on a text file with lines that contain numbers and do not)
(Not Contain chooses zero lines)
I tried passing the function the parameter "sExpr[0]" and it generated an access violation.
The help for the function states:
RegExPos
Syntax
RegExPos (sSub, sSource: String; var sExp: String): Integer
int RegExPos (String sSub, String sSource, String &sExp)
Description
Find a sub string within the source string. The search starts from the beginning of the source string. The found expression is stored in the "sExp" variable. The return value is the position of the found sub string. If nothing is found, the function returns 0 (zero).
The docs are WRONG.
RegExPos returns
-1 if not found,
not 0. (corrected code now works)
I'm not sure why it shows two function templates.
If I use: iFound = ScriptUtils.RegExPos (sSearchtext, sLineText);
It gives me an error of "Not enough actual parameters".
I tried using "/[0-9]+/" as the RegEx, but that didn't change the results.
Anyone know what to do about this??
Code: Select all
// DelLinesContainAndNot.js V1.00
// Need RJ TextEd V10.30 stable or newer
var sSearchtext = "", iDeleted = 0, iLine = 0, iFound, sLineText;
var fInputBox, eInput, btContain, btNotContain, btCancel, cbCount, cbRegEx;
var bContain, sBtCancel, sBtContain, sBtNotContain, sCbCount, sCbRegEx, bCount, bRegEx;
var sInputBoxTitle, sEndMessage, sCountMessage1, sCountMessage2, sContaining, sNotContaining, sFunction;
var sExpr = "";
var sLanguage = "Eng"; // possible Values: Ger, Eng
switch (sLanguage) {
case "Ger":
{
sCbCount = "zählen";
sCbRegEx = "RegEx";
sBtCancel = "Abbruch";
sBtContain = "enthalten";
sBtNotContain = "n. enthalten";
sInputBoxTitle = "Lösche Zeilen welche Text...";
sEndMessage = " Zeilen wurden entfernt.";
sCountMessage1 = " Leitungen ";
sCountMessage2 = " entfernt werden würde.";
sContaining = "contaning";
sNotContaining = "nicht contaning";
}
default: // default to English
{
sCbCount = "Count";
sCbRegEx = "RegEx";
sBtCancel = "Cancel";
sBtContain = "Contain";
sBtNotContain = "Not Contain";
sInputBoxTitle = "Delete lines which text... ?";
sEndMessage = " lines were removed.";
sCountMessage1 = " lines ";
sCountMessage2 = " would be removed.";
sContaining = "containing";
sNotContaining = "not containing";
}
} // switch
inputBoxForm (sInputBoxTitle, Document.SelText);
if (sSearchtext != "") {
if (!bCount) {
Document.BeginUpdate();
}
while (iLine < Document.LineCount) {
sLineText = Lowercase (Document.Lines[iLine]);
if (bRegEx) {
iFound = ScriptUtils.RegExPos (sSearchtext, sLineText, sExpr);
} else {
iFound = ScriptUtils.Pos (sSearchtext, sLineText);
}
if (iFound > 0) {
if (bContain) {
if (!bCount) {
Document.DeleteLine (iLine);
} else {
iLine++;
}
iDeleted++;
} else {
iLine++;
}
} else {
if (bContain) {
iLine++;
} else {
if (!bCount) {
Document.DeleteLine(iLine);
} else {
iLine++;
}
iDeleted++;
}
}
}
if (!bCount) {
Document.EndUpdate();
ShowMessage (IntToStr (iDeleted) + sEndMessage);
} else {
ShowMessage (IntToStr (iDeleted) + sCountMessage1 + sFunction + sCountMessage2);
}
} // if sSearchtext != ""
function buttonClick (psSender)
{
fInputBox.ModalResult = mrOk;
if (psSender.Name == "Contain") {
if (eInput.Text != "") {
sSearchtext = Lowercase (eInput.Text);
bContain = true;
bCount = cbCount.checked;
bRegEx = cbRegEx.checked;
if (bContain) {
sFunction = sContaining;
} else {
sFunction = sNotContaining;
}
}
}
if (psSender.Name == "NotContain") {
if (eInput.Text != "") {
sSearchtext = Lowercase (eInput.Text);
bContain = false;
bCount = cbCount.checked;
bRegEx = cbRegEx.checked;
if (bContain) {
sFunction = sContaining;
} else {
sFunction = sNotContaining;
}
}
}
} // buttonClick
function inputBoxForm (psTitle, psDefault)
{
fInputBox = new TForm (nil);
fInputBox.Height = 120;
fInputBox.Width = 300;
fInputBox.Caption = psTitle;
fInputBox.BorderStyle = bsDialog;
fInputBox.Position = poScreenCenter;
eInput = new TEdit (fInputBox);
eInput.Parent = fInputBox;
eInput.Left = 10;
eInput.Top = 10;
eInput.Width = 275;
eInput.Text = psDefault;
cbCount = TCheckBox.Create (fInputBox);
cbCount.Parent = fInputBox;
cbCount.Caption = sCbCount;
cbCount.Left = 10;
cbCount.Top = 37;
cbRegEx = TCheckBox.Create (fInputBox);
cbRegEx.Parent = fInputBox;
cbRegEx.Caption = sCbRegEx;
cbRegEx.Left = 80;
cbRegEx.Top = 37;
btContain = new TButton (fInputBox);
btContain.Name = "Contain";
btContain.Parent = fInputBox;
btContain.SetBounds (65, 60, 70, 25);
btContain.Caption = sBtContain;
btContain.Default = true;
btContain.OnClick = &buttonClick;
btNotContain = new TButton (fInputBox);
btNotContain.Name = "NotContain";
btNotContain.Parent = fInputBox;
btNotContain.SetBounds (140, 60, 70, 25);
btNotContain.Caption = sBtNotContain;
btNotContain.OnClick = &buttonClick;
btCancel = new TButton (fInputBox);
btCancel.Name = "Cancel";
btCancel.Parent = fInputBox;
btCancel.SetBounds (215, 60, 70, 25);
btCancel.Caption = sBtCancel;
btCancel.OnClick = &buttonClick;
fInputBox.ShowModal;
fInputBox.Free;
} // inputBoxForm