Goto longest line

Help with writing and running scripts

Goto longest line

Postby jrs317 » Thu Apr 09, 2009 6:45 am

Hi UltraEdit users,
sometimes I need a function like 'goto longest line'.
Actually (in UE 14.20) I try to find them by scrolling down with the page down key :-(
Can I find them with a regular expression or is there a function built in?

Thanks, jrs
jrs317
Newbie
 
Posts: 4
Joined: Thu Apr 09, 2009 6:30 am

Re: Goto longest line

Postby Mofi » Tue Apr 14, 2009 2:49 am

There is no built-in function for that task. But following little script works for files not containing tabs. A tab is 1 character, but displayed with a variable number of spaces. But maybe your files do not contain tabs and therefore the script is already enough for you.

Code: Select all
// This script sets the cursor to start of the first line in the active
// document with the greatest number of characters. If the file does not
// contain tabs, this line is also the first line of the longest lines
// or the longest line if there is no further line with the same number
// of characters.
if (UltraEdit.document.length > 0) {
   
   var nChars = 0;
   var nFound = 0;
   var nLine  = 0;
   var nStep  = 1000;

   UltraEdit.perlReOn();
   UltraEdit.activeDocument.hexOff();
   UltraEdit.activeDocument.findReplace.searchDown=true;
   UltraEdit.activeDocument.findReplace.searchInColumn=false;
   UltraEdit.activeDocument.findReplace.mode=0;
   UltraEdit.activeDocument.findReplace.matchCase=false;
   UltraEdit.activeDocument.findReplace.matchWord=false;
   UltraEdit.activeDocument.findReplace.regExp=true;

   do {
      nFound = nChars;
      nChars += nStep;
      UltraEdit.activeDocument.top();
      if (UltraEdit.activeDocument.findReplace.find("^.{"+nChars+",}")) {
         nLine = UltraEdit.activeDocument.currentLineNum;
      } else {
         if (nStep == 1) break;
         nStep /= 10;
         nChars = nFound;
      }
   } while (nChars < 20000);

   UltraEdit.activeDocument.gotoLine(nLine,1);
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Goto longest line

Postby jrs317 » Wed Apr 15, 2009 9:45 am

thanks, this Skript works fine for me.
best regards
jrs
jrs317
Newbie
 
Posts: 4
Joined: Thu Apr 09, 2009 6:30 am

Re: Goto longest line

Postby jrs317 » Wed Apr 15, 2009 10:01 am

.. it's cool.
thanks again for your time
jrs
jrs317
Newbie
 
Posts: 4
Joined: Thu Apr 09, 2009 6:30 am

Re: Goto longest line

Postby Mofi » Thu Apr 16, 2009 2:40 am

Well, I was not satisfied with my first version. It scrolls too much and searches too often the same area for the longest line. I thought a little and found out, that with changing the initial value of 1 variable, deleting one line and moving another line up, the script runs MUCH faster because scrolling is reduced to an absolute minimum and searching runs on the same line as often as possible. Here is the speed improved second version:

Code: Select all
// This script sets the cursor to start of the first line in the active
// document with the greatest number of characters. If the file does not
// contain tabs, this line is also the first line of the longest lines
// or the longest line if there is no further line with the same number
// of characters.
if (UltraEdit.document.length > 0) {
   
   var nChars = 0;
   var nFound = 0;
   var nLine  = 1;
   var nStep  = 1000;

   UltraEdit.perlReOn();
   UltraEdit.activeDocument.hexOff();
   UltraEdit.activeDocument.findReplace.searchDown=true;
   UltraEdit.activeDocument.findReplace.searchInColumn=false;
   UltraEdit.activeDocument.findReplace.mode=0;
   UltraEdit.activeDocument.findReplace.matchCase=false;
   UltraEdit.activeDocument.findReplace.matchWord=false;
   UltraEdit.activeDocument.findReplace.regExp=true;

   do {
      nFound = nChars;
      nChars += nStep;
      UltraEdit.activeDocument.gotoLine(nLine,1);
      if (UltraEdit.activeDocument.findReplace.find("^.{"+nChars+",}")) {
         nLine = UltraEdit.activeDocument.currentLineNum;
      } else {
         if (nStep == 1) break;
         nStep /= 10;
         nChars = nFound;
      }
   } while (nChars < 20000);
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Goto longest line

Postby jrs317 » Thu Apr 16, 2009 9:06 am

This faster one leads to another feature. I've replace one line to get the "find next" Feature. Your modified algorithm makes this possible. Now I cought them all with a few clicks. :D

Code: Select all
//Modification: 
  var nLine  = UltraEdit.activeDocument.currentLineNum+1;
jrs317
Newbie
 
Posts: 4
Joined: Thu Apr 09, 2009 6:30 am


Return to Scripts