Learning by doing: Loop through all lines

Started by: Stefan1 voices3 posts

No useful script. Just an little lesion.
Loop virtually through all lines in file
The cursor will not move!
We access each line by its number only.

- - -

Best to learn is to do some lessons.
So i will show my steps creating an macro for others.

I am not saying that this is the best way to do it,
it is just an start.

If you have an better way to do it, or other
suggestions, just reply :-)



////Loop virtually through all lines in file
////(Cursor will not move!
//// We access each line by its number only.)

LinesInFile = document.GetLines();
LineToStart = 1;
alert ("Lines in file: " + LinesInFile);

////For Each Line in File Do:
for (var LINE = LineToStart; LINE <= LinesInFile; LINE++)
{
////Get the lines content:
str = document.GetLine(LINE);

////===Example use, add here your own code======
////Show the content and ask to continue:
bAnswer = confirm("Processing line " + LINE
+ ". Content:nn"
+ str + "nnnn"
+ "Continue?" );

////What to do If you pressed cancel?:
if (bAnswer == false){break;}
//use "break; to exit the "for" loop only
//use "quit();" to cancel the whole script
////===END of Example use, =====================
}

alert("All done. Script quits.");
No useful script. Just an little lesion.
Loop virtually through only the selected lines in file.
The cursor will not move!
We access each line by its number only.

- - -

Best to learn is to do some lessons.
So i will show my steps creating an macro for others.

I am not saying that this is the best way to do it,
it is just an start.

If you have an better way to do it, or other
suggestions, just reply :-)



////Loop through Selection

////Object handling to save always typing of 'document.selection':
var d = document;
var s = d.selection;


////////Get the selected text to work on:
////////(Depends if already something is selected or not)
if (s.IsEmpty)
{
////If document.selection is empty:
vWasAnSelection = false;

////Alternative one
//alert("Nothing selected, script quits. Please select and start again.");
//quit();

////Alternative two
alert("Nothing selected, script will select whole text for you.");
s.SelectAll();

}else{

////If there was an selection already:
vWasAnSelection = true;
alert("Selected text to work on: n" + s.text);
}



////////Get start and end line of the selection:
///Returns the line number of the top of the selection.
FirstSelLine = s.GetTopPointY(eePosLogical);
///Returns the line number of the bottom of the selection.
LastSelLine = s.GetBottomPointY(eePosLogical);



////For Each Line In SelectedLineS Do:
for( Line = FirstSelLine; Line <= LastSelLine; Line++)
{
//// <<< Your code here >>>
//// Here an simple example:
ret = Confirm(d.GetLine(Line));
if (ret == false) {if (!vWasAnSelection){s.Collapse();} Quit();}
}


////If there was no selection do an UnSelectAll at the end:
if (!vWasAnSelection) s.Collapse();

Some examples for section:


////Delete start of line (Remove first sign/char):
redraw = false;
document.selection.SetActivePoint( eePosLogical, 1, Line );
str = document.GetLine( Line );
if (str != "")
{
document.selection.StartOfLine();
document.selection.Delete();
}



////Delete end of line (Remove last sign/char):
redraw = false;
document.selection.SetActivePoint( eePosLogical, 1, Line );
str = document.GetLine( Line );
if (str != "")
{
document.selection.EndOfLine();
document.selection.DeleteLeft();
}


########################################

However, for just this task there
would be an easier solution then such an big script as shown above:


//Remove first sign from selected lines
redraw = false;
document.selection.Replace("^.","", eeReplaceSelOnly | eeReplaceAll | eeFindReplaceRegExp);
document.HighlightFind = false;



//Remove last sign from selected lines
redraw = false;
document.selection.Replace(".$","", eeReplaceSelOnly | eeReplaceAll | eeFindReplaceRegExp);
document.HighlightFind = false;


3 posts in this topic