Hi guys,
here is my align script I was unable to do with UE macro language.
Imagine you have (huge) code like this
and want it to be like this (left align)
or that (right align)
So from now on you can call my script.
Here is last version of the script alignEqual.js.
Version 1.3 was posted by me at 20:17 on 2007-10-25 and had also posted code from other forum members.
Thanks to toddm and especially to jorrasdk who helped a lot to write version 1.3 of the script.
Hope it is helpful.
rds Bego
Update by Mofi: Script of Bego revised to work with any version of UltraEdit or UEStudio supporting scripts. See also the alternate script posted below making the alignment in memory instead of a temporary created new file which is faster as it avoids lots of display updates during script execution.
here is my align script I was unable to do with UE macro language.
Imagine you have (huge) code like this
Code: Select all
lala = abc
lalalalala = def
la = ghi
baba = jkl asdf sdsdfas
Code: Select all
lala = abc
lalalalala = def
la = ghi
baba = jkl asdf sdsdfas
Code: Select all
lala = abc
lalalalala = def
la = ghi
baba = jkl asdf sdsdfas
Here is last version of the script alignEqual.js.
Code: Select all
// alignEqual.js
// Bego
// Aligns the SELECTION by the equal sign. Might be configured for Left or Right alignment!
// Version 1.1 25.10.2007 result once was shifting 1 column to the left for each run: pos1 = getColOfFirstChar();// - 1;
// 1.2 25.10.2007 Now MYSIGN can be really sth. else like "=", e.g. "/", but yet still only ONE character.
// 1.3 25.10.2007 Shifting result in 1.1 was problem of configuration of HOME key when pressed twice.
// Made it safe now. CloseFile workaround 13.20+2
// 1.4 03.07.2014 Script revised by Mofi.
// Avoid usage of command setActive() as not working up to UE v14.20.0.
// This script works now with all versions of UE/UES, even with
// UE v13.00 and UES v6.20 although very slow with those versions.
var MYSIGN = "=";
var MYALIGN = "R"; // (L)eft or (R)ight
// Is any file opened and the alignment string is not an empty string?
if ((UltraEdit.document.length > 0) && (MYSIGN.length > 0))
{
// Always select all on testing script during development.
// UltraEdit.activeDocument.selectAll();
// Nothing is done if nothing is selected on script start.
if (UltraEdit.activeDocument.isSel())
{
UltraEdit.insertMode();
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
var pos1 = 0;
var maxColEqual = 0;
// Column number property available since UE v13.10 / UES v6.30.
// A very slow workaround is needed for UE v13.00 and UES v6.20.
var bColProperty = (typeof(UltraEdit.activeDocument.currentColumnNum) == "number") ? true : false;
// Starting with UE v16.00 / UES v10.00 the property currentColumnNum
// contains the column number with number 1 for first column in line.
// Older versions return 0 for first column. This difference must be
// compensated by subtracting 0 or 1 depending on version of UE / UES.
var colStart = (typeof(UltraEdit.activeDocumentIdx) == "undefined") ? 0 : 1;
var sourceDoc = UltraEdit.document[getActiveDocumentIndex()];
UltraEdit.selectClipboard(9);
sourceDoc.copy();
UltraEdit.newFile();
var orgDoc = UltraEdit.activeDocument;
orgDoc.paste();
orgDoc.top();
pos1 = getColOfFirstChar();
stripEqualRemoveLeadingSpaces();
maxColEqual = getMaxColEqual();
stuff(pos1, maxColEqual, MYALIGN);
orgDoc.selectAll();
orgDoc.copy();
sourceDoc.paste();
UltraEdit.clearClipboard();
UltraEdit.selectClipboard(0);
UltraEdit.closeFile(orgDoc.path,2);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
function stuff(pPos1, pMaxCol, pAlign)
{
var col;
var spaces = " ";
orgDoc.top();
// Go through once again and stuff spaces, depending on the
// position of the equal sign and the calculated maxPosition.
while (orgDoc.findReplace.find(MYSIGN))
{
// Cancel the current selection.
orgDoc.key("LEFT ARROW"); orgDoc.key("RIGHT ARROW");
col = getCurrentColumNum();
if (pAlign == "R")
{
orgDoc.key("HOME");
orgDoc.write(spaces.substr(0,pMaxCol - col + pPos1));
}
else
{
// The caret is positioned now at beginning of the found string
// in UE < v14.00 and UES < v6.50. But in all later versions the
// caret is now after the found string and therefore on space
// after the alignment string. Caret must be moved backwards.
if (orgDoc.isChar(" "))
{
for (var nLength = 0; nLength < MYSIGN.length; nLength++)
{
orgDoc.key("LEFT ARROW");
}
}
orgDoc.write(spaces.substr(0,pMaxCol - col));
orgDoc.key("HOME");
orgDoc.write(spaces.substr(0,pPos1));
}
orgDoc.key("END");
}
}
function getMaxColEqual()
{
// Get the most right equal sign in the selection. This is the
// orientation for the stuffing spaces at the beginning of the line.
var maxCol = 0;
var Column = 0;
orgDoc.top();
orgDoc.findReplace.regExp = false;
while (orgDoc.findReplace.find(MYSIGN))
{
if (bColProperty)
{
Column = orgDoc.currentColumnNum - colStart;
}
else
{ // Cancel selection and update column number internally.
orgDoc.key("LEFT ARROW");
orgDoc.key("RIGHT ARROW");
Column = getCurrentColumNum();
}
orgDoc.key("END"); // Continue search in next line.
if (Column > maxCol)
{
maxCol = Column;
}
}
return maxCol;
}
function stripEqualRemoveLeadingSpaces()
{
// ONE space before and after equal sign.
orgDoc.top();
UltraEdit.perlReOn();
orgDoc.findReplace.searchDown=true;
orgDoc.findReplace.matchCase=true;
orgDoc.findReplace.matchWord=false;
orgDoc.findReplace.regExp = true;
orgDoc.findReplace.preserveCase=true;
orgDoc.findReplace.replaceAll = true;
var strFind = "[\\t ]*" + MYSIGN + "[\\t ]*";
orgDoc.findReplace.replace(strFind, " " + MYSIGN + " ");
// Remove all leading tabs and spaces.
orgDoc.findReplace.replace("^[\\t ]*", "");
}
function getColOfFirstChar()
{
// Determines in which column the line starts with content.
// V1.3: Maybe do it twice since HOME key might toggle
// between start of line and start of first char.
orgDoc.key("HOME");
if (orgDoc.isColNum(1))
{
orgDoc.key("HOME");
}
// With configuration setting "Home key always goes to column 1" the
// caret is still not positioned at first non whitespace character.
// This loop moves the caret to first character in first line not
// being a space or a horizontal tab character.
while (orgDoc.isChar(" ") || orgDoc.isChar("\t"))
{
orgDoc.key("RIGHT ARROW");
}
return getCurrentColumNum();
}
function getActiveDocumentIndex()
{
// There is an active document index property since UE v16.00 / UES v10.00.
if (typeof(UltraEdit.activeDocumentIdx) == "number")
{
return UltraEdit.activeDocumentIdx;
}
// Workaround solution for UE < v16.00 and UES < v10.00.
for (var i = 0; i < UltraEdit.document.length; i++)
{
if (UltraEdit.activeDocument.path == UltraEdit.document[i].path)
{
return i;
}
}
return (-1);
}
// Function to work around missing currentColumnNum property in UE v13.00
// and UES v6.20 making this script very slow on execution with those old
// versions of UltraEdit / UEStudio.
function getCurrentColumNum()
{
if (bColProperty)
{
return (orgDoc.currentColumnNum - colStart);
}
var CurColumn = 0;
// isColNum starts column counting with 1 for first column.
while (!orgDoc.isColNum(++CurColumn));
// But returned must be a column number with 0 for first column.
return (--CurColumn);
}
Thanks to toddm and especially to jorrasdk who helped a lot to write version 1.3 of the script.
Hope it is helpful.
rds Bego
Update by Mofi: Script of Bego revised to work with any version of UltraEdit or UEStudio supporting scripts. See also the alternate script posted below making the alignment in memory instead of a temporary created new file which is faster as it avoids lots of display updates during script execution.
Normally using all newest english version incl. each hotfix. Win 10 64 bit