How can I align values to same column on UltraEdit?

501 Views Asked by At

I'm using ultraedit to write a text like following:

key1: value1
key2 with different length: value2
key3 with other length: value3 with other length
key4: guess what? value4

And I would like to have a way (probably via a Macro but not necessarily) to align values to the same column, resulting in a text like this:

key1:                       value1
key2 with different length: value2
key3 with other length:     value3 with other length
key4:                       guess what? value4

Is there a way to do this using UltraEdit? Ideas of how to achieve this same goal in other text editors are also appreciated.

1

There are 1 best solutions below

0
On

UltraEdit macros do not support variables which makes this alignment task difficult to code. It is possible with an UltraEdit macro as demonstrated for example by Column Align Macro. But an UltraEdit script is much better for this task as it can be coded to do the alignment in memory and output the result into the file producing just one undo record if the file respectively the selected block is not too large.

Here is a commented UltraEdit script for this right alignment task on first colon in selected lines or all lines of active file.

if (UltraEdit.document.length > 0)  // Is any file opened?
{
    // Define the separator character (or string) of which first
    // occurrence in a line the right alignment should be done.
    var sSeparator = ":";

    // Define environment for this script.
    UltraEdit.insertMode();
    UltraEdit.columnModeOff();

    var nLineNumber = 0;
    var nColumnNumber = 0;

    // Is there no block selected on starting the script?
    if (!UltraEdit.activeDocument.isSel())
    {
        // Get current line and column number of caret position
        // in active file and select everything in active file.
        nLineNumber = UltraEdit.activeDocument.currentLineNum;
        nColumnNumber = UltraEdit.activeDocument.currentColumnNum;
        UltraEdit.activeDocument.selectAll();
        // This line is for UltraEdit for Windows < v16.00 / UEStudio < v10.00.
        if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nColumnNumber++;
    }

    // Is active file not empty?
    if (UltraEdit.activeDocument.isSel())
    {
        // Determine type of line termination (DOS/Windows or Unix or Mac).
        var sLineTerm = "\r\n";
        if (UltraEdit.activeDocument.lineTerminator == 1) sLineTerm = "\n";
        else if (UltraEdit.activeDocument.lineTerminator == 2) sLineTerm = "\r";

        // Split the selected block into an array of lines.
        var asLines = UltraEdit.activeDocument.selection.split(sLineTerm);

        // Determine the maximum position of the separator in a line.
        // A horizontal tab character is interpreted as one whitespace
        // and not as series of spaces depending on position in line.
        // The loop below could be extended with extra code to take horizontal
        // tabs also into account with a tab stop value defined in the script.
        var nMaxSeparatorPosition = -1;
        for (var nLine = 0; nLine < asLines.length; nLine++)
        {
            var nCurSeparatorPosition = asLines[nLine].indexOf(sSeparator);
            if (nCurSeparatorPosition > nMaxSeparatorPosition)
            {
                nMaxSeparatorPosition = nCurSeparatorPosition;
            }
        }

        // Has at least one line the separator?
        if (nMaxSeparatorPosition >= 0)
        {
            nMaxSeparatorPosition++;  // For one space after separator.

            // Create a string consisting of only spaces depending on
            // maximum position of separator in the selected lines.
            var sSpaces = "";
            for (var nSpace = 0; nSpace < nMaxSeparatorPosition; nSpace++) sSpaces += " ";

            // Define the regular expression find string to realign the
            // lines by using a regular expression string replace.
            var sSearchExp = new RegExp("^([^"+sSeparator+"]*?"+sSeparator+")[\t ]*","g");

            // Align the lines containing the separator.
            // The other lines are kept without any modification.
            for (var nLine = 0; nLine < asLines.length; nLine++)
            {
                nCurSeparatorPosition = asLines[nLine].indexOf(sSeparator);
                if (nCurSeparatorPosition)
                {
                    var sAlignSpaces = sSpaces.substr(0,nMaxSeparatorPosition-nCurSeparatorPosition);
                    asLines[nLine] = asLines[nLine].replace(sSearchExp,"$1"+sAlignSpaces);
                }
            }

            // Overwrite the selected lines with the realigned lines.
            UltraEdit.activeDocument.write(asLines.join(sLineTerm));
        }

        if (nLineNumber)    // Has the script selected all at beginning?
        {
            // Reposition the caret on initial position in active file.
            UltraEdit.activeDocument.gotoLine(nLineNumber,nColumnNumber);
        }

        // Inform the user with a message box on no separator
        // found in the selected block or the entire file.
        if (nMaxSeparatorPosition < 0)
        {
            if (sSeparator == "\t") sSeparator = "tab";
            UltraEdit.messageBox("There is no "+sSeparator+" in "+(nLineNumber ? "file." : "selection."));
        }
    }
}