Page 2 of 2

Re: Example: DelLineContains / DelLineNotContains

Posted: 12 May 2015 22:53
by rjbill
micha_he wrote:Is it possible that all changes in a script (or between 'BeginUpdate' and 'EndUpdate'), recognize as one single restorable change ?

When a script delete 1000 lines in one loop/process and you will undo it, these are many, many steps !
Rickard Johansson wrote:It may be possible to add (in a later version). But you can already undo all of it in one action by using the "Edit -> Undo/Redo History..." window.
What it needs is something like BeginUndoGroup and EndUndoGroup.

Re: Example: DelLineContains / DelLineNotContains

Posted: 13 May 2015 02:46
by rjbill
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

Re: Example: DelLineContains / DelLineNotContains

Posted: 13 May 2015 07:08
by pjj
rjbill wrote:If I use: iFound = ScriptUtils.RegExPos (sSearchtext, sLineText);
It gives me an error of "Not enough actual parameters".
And rightly so, since this function needs three arguments (as per specs!); the third one should be string variable, but there's a catch: you should initialize it as an empty string.
rjbill wrote:I tried using "/[0-9]+/" as the RegEx, but that didn't change the results.
You should drop the slashes and use only "[0-9]+".

To sum it up, this works for me:

Code: Select all

var sSearchtext = "[0-9]+";
var sFoundText = "";
iFound = ScriptUtils.RegExPos(sSearchtext, sLineText, sFoundText);

Re: Example: DelLineContains / DelLineNotContains

Posted: 13 May 2015 08:49
by rjbill
pjj wrote: You should drop the slashes and use only "[0-9]+".

To sum it up, this works for me:

Code: Select all

var sSearchtext = "[0-9]+";
var sFoundText = "";
iFound = ScriptUtils.RegExPos(sSearchtext, sLineText, sFoundText);
That's what I did and it's not working.

The problem was that RegExPos returns -1 if not found, not 0. (the docs are wrong)
Pos returns 0 if not found.


So I changed the condition check to "> 0" and now it works.
Thanks.

Re: Example: DelLineContains / DelLineNotContains

Posted: 13 May 2015 09:14
by pjj
Yup, I already filed that bug. I'm glad the script works now :)

Re: Example: DelLineContains / DelLineNotContains

Posted: 21 Jul 2015 10:58
by micha_he
Update the script 'DelLines(Not)Contain.js' to version V1.02.

Changes:

Document.LineCount contains always >=1 ! Even if the document is empty. Changed termination of the loop...

Re: Example: DelLineContains / DelLineNotContains

Posted: 30 Nov 2017 15:04
by micha_he
@Rickard:

The script didn't work with V12.51 !?
Have you change somthing in the used functions ?

Thanks
Micha

Moment: Now works !? I'll do some tests... I've found it: The file was opened in Monitor-Mode. Is there a solution via script, to recocnize the mode ?