Script for Equal-sign alignment

Help with writing and running scripts

Re: Script for Equal-sign alignment

Postby jorrasdk » Thu Oct 25, 2007 10:36 am

Ok, now I get it:

var strFind = "\\s*" + MYSIGN + "\\s*";
orgDoc.findReplace.replace(strFind, " " + MYSIGN + " ");


this invalidates my trick because you cannot use variables in assignments of Regexp object like:
var myRegexp = /\s*=\s*/;

You could write the following:

var myRegexp = new RegExp ("\\s*"+ MYSIGN +"\\s*");

but then lose the whole point of getting around escaping backslashes. This way you might as well keep on working with "ordinary" strings.
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Script for Equal-sign alignment

Postby Bego » Thu Oct 25, 2007 12:18 pm

I came to the same conclusion: Vars & /perlregexp/ do not like each other.
Anyway it works quite well now.
Maybe I find some time to implement aligning with more than ONE char. Should not be that hard I think ....
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Script for Equal-sign alignment

Postby toddm » Thu Oct 25, 2007 12:56 pm

I still had a problem with 1.2 where the temp file didn't close and had to use jorrasdk's solution from earlier in the thread.

Code: Select all
UltraEdit.closeFile(orgDoc.path,2); //ab UE 13.10
User avatar
toddm
Advanced User
Advanced User
 
Posts: 83
Joined: Wed Jul 28, 2004 11:00 pm

Re: Script for Equal-sign alignment

Postby Bego » Thu Oct 25, 2007 1:17 pm

Had that too. Seems to be a "re-bug". UE 13.20+2 XP sp2.
Here is V. 1.3 with closeFile workaround AND a more robust solution for "Column-shifting feature ;-) " from V. 1.1

I hope you are not annoyed with those many updates. Thx.

Code: Select all
//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;
}
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Script for Equal-sign alignment

Postby jorrasdk » Thu Oct 25, 2007 1:53 pm

Bego wrote:Had that too. Seems to be a "re-bug". UE 13.20+2 XP sp2.


Hmmm if UltraEdit.closeFile(); infact have been working in earlier versions of 13.20 or 13.10 I have missed it.

I think that a closeFile() method, or rather a closeFile(saveMode) method, should be associated with the UltraEdit.document object, so you would write:

UltraEdit.activeDocument.closeFile(saveMode);
or
UltraEdit.document[i].closeFile(saveMode);

UltraEdit.closeFile(path,saveMode) could be preserved for backwards compatibility as a deprecated method.

I think I will suggest this to IDM.
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Script for Equal-sign alignment

Postby Bego » Thu Oct 25, 2007 4:51 pm

OK Jorgen, thx for contacting IDM.

@todd: Version 1.3 supports also already more than one character.
e.g. change the definition line to this:

Code: Select all
var MYSIGN = "<=";

and this:

Code: Select all
  la skdjlfh j    <=  lskdjfh kj
      gf saf<=sdafhkj
   sdaf <=   sdlökfjdafkj
         lskdf <= lskdafjsal
   lksja j     <= lskdafsa
   lkj <= salkj sda
   slkdjgh kkgkfsd       <= sdahskja fskdja sda
   lfkjsda <=    sdakjnlökfjsda

turns to that:
Code: Select all
  la skdjlfh j    <= lskdjfh kj
  gf saf          <= sdafhkj
  sdaf            <= sdlökfjdafkj
  lskdf           <= lskdafjsal
  lksja j         <= lskdafsa
  lkj             <= salkj sda
  slkdjgh kkgkfsd <= sdahskja fskdja sda
  lfkjsda         <= sdakjnlökfjsda

Pls tell me if it works.

rds Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Script for Equal-sign alignment

Postby toddm » Thu Oct 25, 2007 5:24 pm

Works great. Thanks Bego!
User avatar
toddm
Advanced User
Advanced User
 
Posts: 83
Joined: Wed Jul 28, 2004 11:00 pm

Previous

Return to Scripts