Help with script

Ask questions about how to create a script or swap scripts with other users.
Post Reply
Aleks842
Posts: 13
Joined: 02 Apr 2014 16:37

Help with script

Post 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. :)
User avatar
pjj
Posts: 2109
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: Help with script

Post 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.
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
Aleks842
Posts: 13
Joined: 02 Apr 2014 16:37

Re: Help with script

Post by Aleks842 »

I need a script.
User avatar
Rickard Johansson
Site Admin
Posts: 6577
Joined: 19 Jul 2006 14:29

Re: Help with script

Post 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.)
User avatar
pjj
Posts: 2109
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: Help with script

Post 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();
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
Aleks842
Posts: 13
Joined: 02 Apr 2014 16:37

Re: Help with script

Post by Aleks842 »

Thank you! The second script works perfectly.
User avatar
pjj
Posts: 2109
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: Help with script

Post by pjj »

You are very welcome! I'm glad I could help :)
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
Post Reply