Search code examples
texteditfixed-widthemeditor

Autofill space up to fixed Column width on each line


I am trying to set the column width of each line to a fixed "511" width on a large dataset. If the line has less than 511 column width then I want to add space on the end of the line until it has 511 column width. Currently, I am running into a lot of the entries that have irregular column widths. For instance, if the column width is 450 i.e. less than than 511 then I am manually adding space until it has 511 width and if column width exceeds 511 then I am deleting space until it is 511. The current dataset has over 1 million rows so I was wondering if there was faster way to add "spaces" on the end of each line. Would appreciate any help.

Sample of irregular column width


Solution

  • If there aren't many irregular lines (if most lines are 511 characters long), you can use this macro. Otherwise, this macro might take a long time to finish.

    COLUMN = 511 + 1;  // add 1 to the width
    Redraw = false;
    nLines = document.GetLines();
    for( y = 1; y <= nLines; ++y ) {
        document.selection.SetActivePoint( eePosLogical, 1, y );
        document.selection.EndOfLine( false, eeLineLogical );
        x = document.selection.GetActivePointX( eePosLogical );
        if( x < COLUMN ) {
            s = "";
            for( i = COLUMN - x ; i > 0; --i ) {
                s += " ";
            }
            document.selection.Text = s;
        }
        else if( x > COLUMN ) {
            document.selection.SetActivePoint( eePosLogical, COLUMN, y, true );
            document.selection.Delete();
        }
    }
    

    To run this, save this code as, for instance, AutoSpace.jsee, and then select this file from Select... in the Macros menu. Finally, select Run AutoSpace.jsee in the Macros menu.