#10028
ldm
Participant

Hi,
I’ve found a Java code, which makes alignments for the `=` sign (see it below). The code is written for the UltraEdit. Could anyone translate it to a general one so that it could be used in the EmEditor as well?
Thank you!
————————————————————
CODE:
————————————————————
//alignEqual.js
//Bego
//Aligns the SELECTION by the Equal-sign. Might be configured with 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
var orgDoc;
var sourceDoc;
var maxColEqual = 0;
var pos1 = 0;
var myDocNo;
var MYSIGN = “=”;
var MYALIGN = “L”; //(L)eft or (R)ight
sourceDoc = UltraEdit.document[getActiveDocumentIndex()];

UltraEdit.outputWindow.showOutput = true;
UltraEdit.outputWindow.clear();
//UltraEdit.outputWindow.write(“Debug: Start”);
UltraEdit.perlReOn();
//ZUM TEST ALLES MARKIEREN UND KOPIEREN STATT MOVEN:
//UltraEdit.activeDocument.selectAll();
UltraEdit.selectClipboard(8);
UltraEdit.activeDocument.cut();
UltraEdit.newFile();
orgDoc = UltraEdit.activeDocument;
orgDoc.paste();
orgDoc.top();

pos1 = getColOfFirstChar();
//UltraEdit.outputWindow.write(“Debug: ” + pos1);
stripEqualRemoveLeadingSpaces();

maxColEqual = getMaxColEqual();
stuff(pos1, maxColEqual, MYALIGN);

UltraEdit.selectClipboard(8);
orgDoc.selectAll();
orgDoc.copy();
//UltraEdit.closeFile(); //13.20+2 does not work
closeFile();

//UltraEdit.document[3].setActive(); //CRASH. Still a bug !!!
UltraEdit.selectClipboard(8);
sourceDoc.paste();
UltraEdit.selectClipboard(0);

///////////////////////////////////////////////////////////////////////////////////////////////////

function closeFile() {
UltraEdit.selectClipboard(9);
orgDoc.copyFilePath();
orgDoc.top();
orgDoc.paste();
orgDoc.selectToTop();
var CurrentFileName = orgDoc.selection;
orgDoc.deleteText();
UltraEdit.closeFile(CurrentFileName,2);
UltraEdit.clearClipboard();
UltraEdit.selectClipboard(0);
}

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)) && ! orgDoc.isEof() ) {
orgDoc.key(“LEFT ARROW”); orgDoc.key(“RIGHT ARROW”); //unmark selection
col = orgDoc.currentColumnNum; //getCol();
orgDoc.key(“HOME”);
if (pAlign == “R”) {
orgDoc.write(spaces.substring(0,pMaxCol – col + pPos1));
}
else
{
orgDoc.write(spaces.substring(0,pPos1));
//reposition in front of sign and stuff here
orgDoc.findReplace.find(MYSIGN);
orgDoc.key(“LEFT ARROW”);
orgDoc.write(spaces.substring(0,pMaxCol – col));
}
orgDoc.key(“END”);
}
}

function getMaxColEqual() {
//get the mostright equal-sign in the selection. This is the orientation for the stuffing-spaces at the beginning of the line.
var max = 0;
var col = 0;

orgDoc.top();
orgDoc.findReplace.replaceAll = false;
orgDoc.findReplace.regExp = false;
while ( (orgDoc.findReplace.find(MYSIGN)) && ! orgDoc.isEof() ) {
col = orgDoc.currentColumnNum; //getCol();
orgDoc.key(“END”);//continue search in next line
if (col > max) {
max = col;
}
}
return max;
}

function stripEqualRemoveLeadingSpaces() {
//ONE space before and after equal-sign. cut all leading spaces.
orgDoc.top();
orgDoc.findReplace.replaceAll = true;
orgDoc.findReplace.regExp = true;
var strFind = “s*” + MYSIGN + “s*”;
orgDoc.findReplace.replace(strFind, ” ” + MYSIGN + ” “);
orgDoc.findReplace.replace(“^s*”, “”);
}

function getColOfFirstChar() {
//determins in which column the line starts with content. V1.3: maybe do it twice since HOME-key might toggle between Col0 and start of first char
orgDoc.findReplace.regExp = true;
orgDoc.key(“HOME”);
if (orgDoc.currentColumnNum == 0) {
orgDoc.key(“HOME”);
}
return orgDoc.currentColumnNum; //ab 13.10
}

function getActiveDocumentIndex() {
var tabindex = -1; /* start value */

for (i = 0; i < UltraEdit.document.length; i++)
{
if (UltraEdit.activeDocument.path == UltraEdit.document[i].path) {
tabindex = i;
break;
}
}
return tabindex;
}