Page 1 of 1

Help with script

Posted: 16 Apr 2014 08:48
by Aleks842
Hello. Can you help me with a script.
In your programm is the script that delete all spaces in the end of lines. How can I do this with the startspaces?
Sorry for my English. Hope you understand me. :)

Re: Help with script

Posted: 16 Apr 2014 09:12
by pjj
Do you need a script for this (to be able to run it e.g. on each file open or save), or do you need it just one time? Because if the latter, you can simply select all text (Ctrl-A) and hit Shift-Tab a couple of times.

Re: Help with script

Posted: 16 Apr 2014 09:26
by Aleks842
I need a script.

Re: Help with script

Posted: 16 Apr 2014 11:30
by Rickard Johansson
Try this

Code: Select all

// C++ script
{
	string s;
	int i,n;
	int startx,startLine,endx,endLine;
	
	if (Document.SelLength > 0) {
		Document.GetSelection(startx,startLine,endx,endLine);
	} else {
		startLine = 0;
		endLine = Document.LineCount - 1;
	};
	
	Document.BeginUpdate();
	try
	{
		i = startLine;
		while (i <= endLine) {
			
			// Get line from document
			s = Document.Lines[i];
			
			// Remove spaces and write back modified line
			n = 0;
			while ((n <= Length(s)) && (ScriptUtils.PosAt(" ",s,n+1))) { n++; };		
			if (n > 0) {
         	// Remove spaces
            Delete(s,1,n);
			
			   // Write modified line to document
			   Document.Lines[i] = s;
         };
			i++;
		};
	}
	finally
	{
		Document.EndUpdate();
	};
}
(Edited once to add try..finally block.)

Re: Help with script

Posted: 16 Apr 2014 11:47
by pjj
OK, since you need a script, here's one for you; enjoy!

Code: Select all

// JScript

// turn off screen updating
Document.BeginUpdate();

// turn word-wrap off
if (Document.Wordwrap) {
    Document.Wordwrap = false;
}

// remove leading spaces
for (var i = 0; i < Document.LineCount; i++) {
    Document.Lines[i] = ScriptUtils.RegExReplaceAll(Document.Lines[i], "^\\s+", "");
}

// turn on screen updating
Document.EndUpdate();

Re: Help with script

Posted: 16 Apr 2014 14:47
by Aleks842
Thank you! The second script works perfectly.

Re: Help with script

Posted: 16 Apr 2014 15:01
by pjj
You are very welcome! I'm glad I could help :)