Example: DelLineContains / DelLineNotContains

Ask questions about how to create a script or swap scripts with other users.
User avatar
micha_he
Posts: 570
Joined: 24 Jul 2011 12:16
Location: Helmstedt, NDS, Germany

Example: DelLineContains / DelLineNotContains

Post by micha_he »

Here is a sample-script to delete all lines that contain a searchstring (old-version, look third new script):

Code: Select all

// DelLinesContain.js V1.02
// Need RJTED V10.30 stable or greater

var searchtext = Lowercase(InputBox("Suchtext", "Suchtext: ", Document.SelText));
var iDeleted = 0, iLine = 0;

if (searchtext != "") {
	Document.BeginUpdate();
	while(iLine < Document.LineCount) {
		sLineText = Document.Lines[iLine];
		if (ScriptUtils.Pos(searchtext, Lowercase(sLineText)) != 0) {
			Document.DeleteLine(iLine);
			iDeleted++;
		} Else {
			iLine++;
		}
	}
	Document.EndUpdate();
	ShowMessage(IntToStr(iDeleted) + "  Zeilen wurden entfernt.");
}
Here is a sample-script to delete all lines that don't contain the searchstring (old-version, look third new script):

Code: Select all

// DelLinesNotContain.js V1.02
// Need RJTED V10.30 stable or greater

var searchtext = Lowercase(InputBox("Suchtext", "Suchtext: ", Document.SelText));
var iDeleted = 0, iLine = 0;

if (searchtext != "") {
	Document.BeginUpdate();
	while(iLine < Document.LineCount) {
		sLineText = Document.Lines[iLine];
		if (ScriptUtils.Pos(searchtext, Lowercase(sLineText)) == 0) {
			Document.DeleteLine(iLine);
			iDeleted++;
		} Else {
			iLine++;
		}
	}
	Document.EndUpdate();
	ShowMessage(IntToStr(iDeleted) + "  Zeilen wurden entfernt.");
}
You can add them under "Tools\Configure Tools...\Add" as type "Script".

New version:
- Both contain and not contain in one script
- Configurable texts
- Change InputBox() to own modal form
- Working cancel-button

Code: Select all

// DelLines(Not)Contain.js V1.02
// Need RJ TextEd V10.40 Beta1 or newer

var sSearchtext = "", iDeleted = 0, iLine = 0;
var fInputBox, eInput, btContain, btNotContain, btCancel;
var bContain, sBtCancel, sBtContain, sBtNotContain;
var sInputBoxTitle, sEndMessage;

var sLanguage = "Ger"; // possible Values: Ger, Eng
switch (sLanguage) {
	case "Eng": {
		sBtCancel = "Cancel";                  
		sBtContain = "Contain";                
		sBtNotContain = "Not Contain";         
		sInputBoxTitle = "Delete lines which text... ?";         
		sEndMessage = "  lines were removed.";
	}
	case "Ger": {
		sBtCancel = "Abbruch";
		sBtContain = "enthalten";                
		sBtNotContain = "n. enthalten";         
		sInputBoxTitle = "Lösche Zeilen welche Text...";         
		sEndMessage = "  Zeilen wurden entfernt.";
	}
}

InputBoxForm(sInputBoxTitle, Document.SelText);
if (sSearchtext != "") {
	Document.BeginUpdate();
	Document.BeginUndo();
	while(iLine < Document.LineCount ) {
		sLineText = Lowercase(Document.Lines[iLine]);
		if (ScriptUtils.Pos(sSearchtext, sLineText) != 0) {
			if (bContain) {
				if (Document.LineCount == 1) { // LineCount ist immer >= 1 !
					iLine++;
				}
				Document.DeleteLine(iLine);
            	iDeleted++;
			} else {
				iLine++;
			}
		} else {
			if (bContain) {
				iLine++;
			} else {
				if (Document.LineCount == 1) { // LineCount ist immer >= 1 !
					iLine++;
				}
				Document.DeleteLine(iLine);
            	iDeleted++;
			}
		}
	}
	Document.EndUndo();
	Document.EndUpdate();
	ShowMessage(IntToStr(iDeleted) + sEndMessage);
}

function ButtonClick(Sender)
{
	fInputBox.ModalResult = mrOk;
	if (Sender.Name == "Contain") {
		if (eInput.Text != "") {
			sSearchtext = Lowercase(eInput.Text);
			bContain = true;			
		}
	}
	if (Sender.Name == "NotContain") {
		if (eInput.Text != "") {
			sSearchtext = Lowercase(eInput.Text);
			bContain = false;			
		}
	}
}

function InputBoxForm(sTitle, sDefault)
{
	fInputBox = new TForm(nil);
	fInputBox.Height = 120;
	fInputBox.Width = 300;
	fInputBox.Caption = sTitle;
	fInputBox.BorderStyle = bsDialog;
	fInputBox.Position = poScreenCenter;
	
	eInput = new TEdit(fInputBox);
	eInput.Parent = fInputBox;
	eInput.Left = 10;
	eInput.Top = 10;
	eInput.Width = 275;
	eInput.Text = sDefault;
	
	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;
}
Last edited by micha_he on 21 Jul 2015 10:53, edited 14 times in total.
User avatar
pjj
Posts: 2109
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: Example: DelLineContains / DelLineNotContains

Post by pjj »

Works perfectly, thanks! :)
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
User avatar
micha_he
Posts: 570
Joined: 24 Jul 2011 12:16
Location: Helmstedt, NDS, Germany

Re: Example: DelLineContains / DelLineNotContains

Post by micha_he »

Code: Select all

Document.SelectLine(i);
Document.DeleteCurrentLine();
Exists a simpler/quicker method to delete a line with the index# (without select it) ?
Maybe a function (i didn't found anything in the helpfile) to set the currentline, instead of select it !

Or is it possible to extend the 'Document.DeleteCurrentLine' to 'Document.DeleteLine(Index)' ? And without given index, delete the current line ?

Edit:

If found a solution: Instead of 'Document.SelectLine(i)', maybe better/quicker 'Document.CursorY = i;'.
I've corrected the script's in post #1.

But 'Document.DeleteLine(Index)' would have the advantage, that the cursor does not have to be moved.
User avatar
Rickard Johansson
Site Admin
Posts: 6577
Joined: 19 Jul 2006 14:29

Re: Example: DelLineContains / DelLineNotContains

Post by Rickard Johansson »

I've added the method "Document.DeleteLine(int line)" to v10.30.
User avatar
micha_he
Posts: 570
Joined: 24 Jul 2011 12:16
Location: Helmstedt, NDS, Germany

Re: Example: DelLineContains / DelLineNotContains

Post by micha_he »

Great :P
User avatar
micha_he
Posts: 570
Joined: 24 Jul 2011 12:16
Location: Helmstedt, NDS, Germany

Re: Example: DelLineContains / DelLineNotContains

Post by micha_he »

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 !
User avatar
Rickard Johansson
Site Admin
Posts: 6577
Joined: 19 Jul 2006 14:29

Re: Example: DelLineContains / DelLineNotContains

Post by Rickard Johansson »

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.
User avatar
micha_he
Posts: 570
Joined: 24 Jul 2011 12:16
Location: Helmstedt, NDS, Germany

Re: Example: DelLineContains / DelLineNotContains

Post by micha_he »

I've modified the scripts from post #1 to the new method 'Document.DeleteLine()'.

Works great and quicker :P
Rickard Johansson wrote:But you can already undo all of it in one action by using the "Edit -> Undo/Redo History..." window.
There I can also undo only one after the other! Or how can I mark all ? Klick on the last and undo, also undo the following and not the first in history-queue !
User avatar
Rickard Johansson
Site Admin
Posts: 6577
Joined: 19 Jul 2006 14:29

Re: Example: DelLineContains / DelLineNotContains

Post by Rickard Johansson »

Try to select the undo point you wish to revert to and press the "Revert" button.
User avatar
micha_he
Posts: 570
Joined: 24 Jul 2011 12:16
Location: Helmstedt, NDS, Germany

Re: Example: DelLineContains / DelLineNotContains

Post by micha_he »

I've tested it... Works !

'Undo' and 'Revert' (German: 'Rückgängig' and 'Zurücknehmen') are not clearly, in my opinion...

Is there a marked difference between undo and revert ?
User avatar
Rickard Johansson
Site Admin
Posts: 6577
Joined: 19 Jul 2006 14:29

Re: Example: DelLineContains / DelLineNotContains

Post by Rickard Johansson »

Revert, in this case, means "go back to a previous state". Undo will simply undo the last operation.

If anyone have a suggestion on how to make the undo/redo history more intuitive - I'm all ears.
User avatar
micha_he
Posts: 570
Joined: 24 Jul 2011 12:16
Location: Helmstedt, NDS, Germany

Re: Example: DelLineContains / DelLineNotContains

Post by micha_he »

Back to my script from post #1:

Is there a way to query that the user had canceled the inputbox ?
If i click cancel, the returned value is the default value and not empty!
User avatar
rjbill
Posts: 874
Joined: 13 Jun 2011 06:36

Re: Example: DelLineContains / DelLineNotContains

Post by rjbill »

For people who speak normal languages like English: :-D

Suchtext = Search text
Zeilen wurden entfernt = Lines were removed

Is there a "best location" to put these scripts in?
(for good housekeeping and such -- never mind -- found:
C:\Users\username\AppData\Roaming\RJ TextEd\Scripts\My Scripts)

Does this support regular expressions??

And I'm confused: WHAT happens if the Cancel button is clicked??

Thanks for doing this.

btw -- It may not be noticeably faster, but if you lowercase the search text once
instead of at every compare, that would be better:

var searchtext = Lowercase (InputBox("SearchText", "Search text: ", Document.SelText));

and

if (ScriptUtils.Pos (searchtext, Lowercase (sLineText)) != 0) {

(also, not a good programming practice to use single-letter variable names,
so at least "ii" is better than "i", although a descriptive name is even better,
like "iLine")
RJTE version 16.15 (actual?) - 64-bit
Win 10 Pro 64-bit 8 GB RAM Intel Core i7-6700 3.40 GHz SCSI Hard Drive 1 TB

Note: The signature is dynamic, not static,
so it may not show the correct version above
that was in use at the time of the post.
User avatar
micha_he
Posts: 570
Joined: 24 Jul 2011 12:16
Location: Helmstedt, NDS, Germany

Re: Example: DelLineContains / DelLineNotContains

Post by micha_he »

rjbill wrote:Does this support regular expressions??
You can change the line with 'if (ScriptUtils.Pos...' to 'if (ScriptUtils.RegExPos...'. But in this case, you must escape the special regex-characters (like \*.? etc.) !
rjbill wrote:And I'm confused: WHAT happens if the Cancel button is clicked??
Look my question in post two above...
But in the new version from post #1, you can clear the inputbox and click ok, if you would cancel the operation.
rjbill wrote:It may not be noticeably faster, but if you lowercase the search text once
instead of at every compare, that would be better:

var searchtext = Lowercase (InputBox("SearchText", "Search text: ", Document.SelText));

and

if (ScriptUtils.Pos (searchtext, Lowercase (sLineText)) != 0) {
Yes good improvement!
rjbill wrote:also, not a good programming practice to use single-letter variable names,
so at least "ii" is better than "i", although a descriptive name is even better,
like "iLine"
In most programming languages is 'i' a predefinied loop-counter. You must not declare it e.g. in AutoIt.
But your right: 'iLine' is better !

Thanks...
User avatar
micha_he
Posts: 570
Joined: 24 Jul 2011 12:16
Location: Helmstedt, NDS, Germany

Re: Example: DelLineContains / DelLineNotContains

Post by micha_he »

New combined version in post#1.

Are in this forum no Spoiler's for code-snippets available ?
Post Reply