Here is an optimized version of the script without right aligning the line numbers with zeros or spaces.
Code: Select all
if (UltraEdit.document.length > 0) // Is any file opened?
{
// Define environment for this script.
UltraEdit.insertMode();
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
var nStepLines = UltraEdit.getValue("Place Line Numbers Every X Number of Lines.\nDefault is every 5 lines.\nPlease enter the number you want.",1);
if (!nStepLines) nStepLines = 5; // Set default here if prompt is set to zero
var nLineNumber = nStepLines;
while (true)
{
UltraEdit.activeDocument.gotoLine(nLineNumber,1);
if (UltraEdit.activeDocument.currentLineNum != nLineNumber) break;
if (UltraEdit.activeDocument.isEof()) break;
UltraEdit.activeDocument.write(nLineNumber+"\t");
nLineNumber += nStepLines;
}
UltraEdit.activeDocument.top();
}
Both IF conditions inside the loop are necessary to avoid an endless running loop in some cases and avoid inserting a line number after last line ending at end of file and avoid inserting a line number at beginning of last line in file which is not a multiple of nStepLines.
The line you don't understand first converts the integer line number of last line in file like
1842 into a string
"1842" and replaces all digits in this string by
0 or a space. So depending on number of last line in file, the string assigned to variable
sAlign has one, two, three, four, ... zeros or spaces. If the line number of current line converted to a string is shorter than the string of
sAlign, the last characters of this string depending on length of current line number string are used to right align the current line number. In other words on a file with
1842 lines,
sAlign is after this line a string variable with string value
0000 and for example for current line being
50, the last two
0 of
sAlign are copied to a new string concatenated with string
"50" to build the string
"0050" to write into the file.