如何在UltraEdit上将值对齐到同一列



我正在使用ultraedit编写如下文本:

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

我想有一种方法(可能通过宏,但不一定(将值对齐到同一列,生成这样的文本:

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

有没有一种方法可以使用UltraEdit做到这一点?如何在其他文本编辑器中实现相同目标的想法也受到赞赏。

UltraEdit宏不支持变量,这使得此对齐任务难以编码。使用UltraEdit宏是可能的,例如列对齐宏。但UltraEdit脚本更适合此任务,因为它可以被编码为在内存中进行对齐,并将结果输出到文件中,如果所选块的文件不太大,则只生成一个撤消记录。

这是一个有注释的UltraEdit脚本,用于在活动文件的选定行或所有行中的第一个冒号上执行此右对齐任务。

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 = "rn";
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."));
}
}
}

最新更新